1
When dispatching a job in Laravel 5.6, send parameters to it. In the constructor, the parameters are received normally, containing the data sent. However, when executing the job with the php artisan queue:work
, one of the parameters is empty in Handler.
Code that dispatches the job:
public function enviar($id) {
$evento = Evento::find($id);
$subscribers = ParticipantesEvento::join('users', 'participantes_eventos.user_id', '=', 'users.id')
->select('users.name', 'users.email')
->where('evento_id', '=', $evento->id)
->get();
$certificate = Certificado::where('evento_id', '=', $evento->id)->first();
dispatch(new SendCertificatesByEmail($subscribers, $evento, $certificate));
return redirect()->back()->with('success', 'Processando...');
}
Follow the (simplified) job code:
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
class SendCertificatesByEmail implements ShouldQueue {
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $subscribers, $events, $certificate;
public function __construct($s, $e, $c) {
$this->subscribers = $s;
$this->evento = $e;
$this->certificate = $c;
print_r($this->subscribers); // Tem dados
print_r($this->evento); // Tem dados
print_r($this->certificate); // Tem dados
}
public function handle() {
print_r($this->subscribers); // VAZIO!!!
print_r($this->evento); // Tem dados
print_r($this->certificate); // Tem dados
}
}
In the Laravel documentation, it is directed to declare the variables in the job as "protected", as I am doing. Even so, I have tried using them as "public". Same problem. Moreover, only one of the variables "becomes" empty in Handler. Any hint??
André, put the code in which you call this job.
– André Lins
@Andrélins Editei
– André Raubach
By printing the variable in the constructor: [{"name":"Andre Raubach","email":"[email protected]"},{"name":"Tester","email":"[email protected]"}] . When printing on Handler: []
– André Raubach
Try to take only one item in the return of subscribers, it may be q there is a problem with array.
– André Lins
Another possibility is that if you are keeping his reference.
– André Lins
I have a project that does it without any problem, I’m trying to see if I can force this error somehow.
– André Lins
I tried to get only one item tbm. When printing inside Handler gives error (pq the empty array ta)
– André Raubach
Solved! I got the solution here: https://stackoverflow.com/questions/55582181/laravel-queue-data-not-being-passed-constructor-to-handler/55583573#55583573
– André Raubach