You should declare these variables public.
Data not being passed to queued job
I'm trying to send a contact email using a queued job but the construct variables are not being passed through. Was wondering if someone can take a look and see what I might be missing. This is Laravel 5.1.19 Controller
public function postContact(ContactFormRequest $request, Config $config)
{
$data = [
'firstName' => $request->get('first_name'),
'lastName' => $request->get('last_name'),
'email' => $request->get('email'),
'message' => $request->get('message'),
'queue' => $config->get('config.beanstalkd.default')
];
$this->dispatchFromArray(SendContactEmail::class, $data);
return redirect()->route('home')->with('success', trans('pages.contact_success'));
}
Job
namespace App\Jobs;
use App\Jobs\Job;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Bus\SelfHandling;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Contracts\Mail\Mailer;
use Illuminate\Contracts\Config\Repository as Config;
class SendContactEmail extends Job implements SelfHandling, ShouldQueue
{
use InteractsWithQueue, SerializesModels;
public function __construct($firstName, $lastName, $email, $message, $queue)
{
$this->firstName = $firstName;
$this->lastName = $lastName;
$this->email = $email;
$this->emailMessage = $message;
$this->onQueue($queue);
}
public function handle(Config $config, Mailer $mailer)
{
$emailAddress = $config->get('mail.from.address');
$emailName = $config->get('mail.from.name');
$data = [
'firstName' => $this->firstName,
'lastName' => $this->lastName,
'email' => $this->email,
'emailMessage' => $this->emailMessage,
];
$mailer->send('emails.contact', $data, function ($message) use ($emailAddress, $emailName) {
$message->to($emailAddress, $emailName)
->from($emailAddress, $emailName)
->subject(trans('emails.contact_subject'));
});
return;
}
}
The variables passed into the construct never get passed to the handler. If I substitute first name, last name, etc. with actual string values, the job runs and sends an email. If not, I see the job in the queue and then it shows in the failed_jobs table:
{"job":"Illuminate\\Queue\\CallQueuedHandler@call","data":{"command":"O:25:\"App\\Jobs\\SendContactEmail\":3:{s:5:\"queue\";s:11:\"default-new\";s:5:\"delay\";N;s:6:\"\u0000*\u0000job\";N;}"}}
Log
'Undefined property: App\Jobs\SendContactEmail::$firstName'
I'm sending the queue as a variable because I was unable to figure out a way to use dispatchFrom with onQueue. Also could not get information passed if using $this->dispatch.
Doing a dump on the variables passed to the construct shows they are getting there. However, it seems Laravel is not picking them up and passing them to the handler.
Any ideas?
Please or to participate in this conversation.