Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

CreekJumper's avatar

Issue with laravel system. (php artisan)

Hello, I'm not sure what I have done, but I seem to now have an issue with my laravel system -- Any time I issue ANY artisan command(seed, clear) or even composer dump autoload.. , I get the following error (below). I'm not sure why is trying to execute this code, can someone point me in the right direction please?

 php artisan cache:clear          
                                                    
  ErrorException                       
                                                    
 Trying to get property 'code_value' of non-object            
                                                                                                    
 at C:\laragon\www\code\app\Code.php:33                       
   29|     {                                                                                    
   30|                                                                                          
   31|         return Code::where('major_code',$majorCode) 
   32|                                         ->where('minor_code',$minorCode)
 > 33|                         ->first()->code_value;                                         
   34|                                                                                                        
   35|     }                                                                                                 
   36|                                                                                                       
   37|     public static function getCodeDescription($majorCode, $minorCode, $codeValue)   
                                                                                                                           
 1   C:\laragon\www\code\app\Code.php:33                                                    Illuminate\Foundation\Bootstrap\HandleExceptions::handleError
("Trying to get property 'code_value'
 of non-object", "C:\laragon\www\code\app\Code.php", ["GENERAL", "TIMEZONE"])               
 2   C:\laragon\www\code\app\Console\Commands\ProcessRecurringTransactions.php:52  
     App\Code::getCodeValue("GENERAL", "TIMEZONE")    
0 likes
5 replies
tykus's avatar

Does the ProcessRecurringTransactions command class have a constructor that makes a query using the Code model class? All command constructors are executed whenever the app is running in the console (i.e. running any artisan command).

CreekJumper's avatar

It certainly does!

 $this->timeZoneLocal = Code::getCodeValue('GENERAL','TIMEZONE');

So if I understand correctly, because its in the constructor, any artisan command will require this record present?

tykus's avatar
tykus
Best Answer
Level 104

Not exactly; Laravel needs to instantiate every console command in order to match the signature (which is a protected property on the Class). In doing so, the constructor is invoked (obviously). So, because you have a query inside the constructor of one of the commands, that query is being executed.

Consider either (i) moving the query into the handle method, or (ii) coding defensively, i.e. do not assume you will get a Code instance and return a default :

return Code::where('major_code',$majorCode) 
	->where('minor_code',$minorCode)
	->first()->code_value ?? null;   
CreekJumper's avatar

You were correct my friend. Do you know why laravel instantiates commands on a cache clear? Just trying to understand.

tykus's avatar

Because cache:clear is an Artisan command and

All command constructors are executed whenever the app is running in the console

... i.e. an Artisan command like cache:clear

Please or to participate in this conversation.