Search code examples
phplaravelthislumen

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 App\Http\Helpers;
    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 App\Http\Helpers\check_id()


Solution

  • 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)