Laravel Undefined property in job

Viewed 3685

In a Laravel job I have:

use Spatie\Valuestore\Valuestore;

and

public function __construct()
{
  $this->settings = Valuestore::make(storage_path('app/settings.json'));
}

and

public function handle()
{
  if($this->settings->get('foo') == 'test') {
etc...

and on this I get an error Undefined property App\Jobs\MyJobName::$settings. What is going wrong?

Even if I do this:

 public function handle()
    {
    $this->settings = Valuestore::make(storage_path('app/settings.json'));
      if($this->settings->get('foo') == 'test') {
    etc...

I get the same error.

Update based on the comments

MyJobName is called in a custom artisan command, that happens to also use Valuestore but I assume that would unrelated.

In the class CustomCommand:

use Spatie\Valuestore\Valuestore;

and

public function __construct()
{
  parent::__construct();
  $this->settings = Valuestore::make(storage_path('app/settings.json'));
}

and

public function handle()
{
  if($this->settings->get('foo') == 'test') // This works in this custom command!
  {
    $controller = new MyController;
    MyJobName::dispatch($controller);
  }
}

So in CustomCommand I use Valuestore in exactly the same way as in MyJobName but in the latter it doesn't work. As per one of the comments: I do not make $this->settings global as I don't do that in CustomCommand either and it works fine there.

Update 2

If I add protected $settings; above the __construct() function as per the comments it still doesn't work, same error.

4 Answers

Just declare the settings property as public in your Job Class.

public $settings;
public function __construct()
{
  $this->settings = Valuestore::make(storage_path('app/settings.json'));
}

I recently had this error. I've tried to make the variables public, delete all the variable inside the Jobs class and even rename and delete the class itself. But it didn't work.

Shortly, I run this artisan command php artisan optimize:clear to clear all the caches, views, routes, etc. And it somehow solve the problem about variable in my problem. For anyone who is still have this OP's problem, give a try to my solution above.

If you use JOB by QUEUE, you need all the requests or SQL queries to do by the method handle

public function handle()
{
  $this->settings = Valuestore::make(storage_path('app/settings.json'));

  ....
}

Because the constructor works when you make the object of class, and this object is serialized and stored in the database and after the unserialization and the handle is triggered.

You may need to restart your queue worker

From Laravel documentation

Remember, queue workers are long-lived processes and store the booted application state in memory. As a result, they will not notice changes in your code base after they have been started. So, during your deployment process, be sure to restart your queue workers.

If you use a daemon php artisan queue:restart

If you use queue:work on your bash hit Ctrl+C then again php artisan queue:work should be enough

Related