Please consider the following job:
<?php
namespace App\Jobs;
use Illuminate\Contracts\Bus\SelfHandling;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class ImportUsers extends Job implements SelfHandling, ShouldQueue
{
use InteractsWithQueue, SerializesModels;
public function __construct($number)
{
$this->number=$number;
}
public function handle()
{
dd($this->number);
return;
}
}
Dispatching this job using a sync
queue $this->dispatch(new \App\Jobs\ImportUsers(5));
throw this exception: Undefined property: App\Jobs\ImportUsers::$number
. This really seems odd for me. Why the handle method can not access class properties?
Properly declare your property
class ImportUsers extends Job implements SelfHandling, ShouldQueue
{
use InteractsWithQueue, SerializesModels;
protected $number; // <-- Here
public function __construct($number)
{
$this->number=$number;
}
public function handle()
{
dd($this->number);
return;
}
}
What happens is after the jobs is being deserialized from the queue you loose dynamically created property.
Try it:
$ php artisan tinker >>> Bus::dispatch(new App\Jobs\ImportUsers(7)); 7 >>>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With