Skip to content
Advertisement

Lumen : using helper get error : Using $this when not in object context

I have create this helper in Helpers folder inside Http folder:

<?php
   namespace AppHttpHelpers;
    class CreateRandomId{
        public static function get_id($my_table)
        {
            $id = mt_rand(1000000000, 9999999999); 
            if($this->check_id($id,$my_table)){
                get_id($my_table);
            }
            return $id;
        }
    
        public static function check_id($id,$my_table)
        {
            $table=DB::table($my_table)->where('id',$id)->get();
            if (count($table)==0){
                return false;
            }
            return true;
        }
    }

when I call it in controller :

$new_user_id = CreateRandomId::get_id('users'); 

it give me this error : Using $this when not in object context where I remove $this in function I get this error : Call to undefined function AppHttpHelperscheck_id()

Advertisement

Answer

check_id is a static function if you want to access it into other function then you have to use…

self::check_id($id,$my_table)
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement