The object is missing when using Laravel Mail :: later

I have this code:

$data = ['user'=>User::find(1)];
Mail::later(5, 'email.template', $data, function($message){
  ...
})

And email.templatethey have the following:

print_r($user)

And when I received the email, the instance of $ user is not a User object. How could this happen? It looks like the object is not referencing the correct context. But when I use Mail :: send, it works fine. Confuse a little here.

+4
source share
3 answers

The vibrant model is too large for a queue job, which typically has a limit of 64 KB.

I would advise you to use the usual queue task, pass it your user ID and let go of the message from there:

$user_id = 1;

Queue::later(5, function () use ($user_id) {
    $data = ['user' => User::find($user_id)];

    Mail::send('email.template', $data, function ($message) {
        // ......
    });
});
+10
source

, . $view- > getData(), Eloquent. Mail:: later Mail:: queue.

View::creator('email.template', function($view)
{
    $view_data = $view->getData();

    if (isset($view_data['user_id']))
    {
        $user_id = $view_data['user_id'];
        $view->with('user', User::find(user_id));
    }
});
+3

Serializing Eloquent models can be a bit complicated. The safest way is to simply pass the identifier to the Mail queue and receive it when it returns:

$data = ['user_id' => 1];
Mail::later(5,'email.template', $data, function ($message) {
   ...
});

And in your template (above):

<?php $user = User::find($user_id); ?>

.. rest of template

Or, as some other people on IRC suggest, skip Mail::later()altogether and use Queue::later()instead. Write your own handler that extracts the models from the identifiers and then launches Mail::send().

+2
source

All Articles