Laravel: passing data with Mail:queue() to a view - developed.be

  • Published:May 7th, 2014
  • Category:Lavarel
  • 8 comments

Mail:send() and Mail:queue() don’t work the same when it comes to passing $data to a view. At least not when you’re passing an eloquent object (or a “model” object).

Eg:

$data = array();
$data['myObject'] = $eloquentObject;
 
Mail::send('emails.hello', $data, function($message) use ($toAddress) {
...
});

This will pass $data to the view so you can access $myObject in the view.

But when you change Mail::send to Mail::queue the $myObject isn’t accessible as expected. This happens when you’re passing an eloquent (inherited) object. To make this work with queue you have to serialize the $eloquentObject first and later unserialize it in the view.

$data = array();
$data['myObject'] = serialize($eloquentObject);
 
Mail::send('emails.hello', $data, function($message) use ($toAddress) {
...
});

In the view:

<?php $serializedObject = unserialize($myObject); ?>
 
{{ $serializedObject->property }}


Rss Comments

8 comments

  1. Wow thank you so much. I was really scratching my head.

    #1 Grant
  2. Hi! this is very annoying, ¿know you the reason of this behavior?
    I can’t find anything related to this on Laravel’s documentation.

    #2 MasterZero
  3. Thanks a lot! :)
    It words.

    By the way, is it a Laravel Bug? And what serialize does for it to be solved?

    #3 marlo
  4. Thanks for this man – wasted about 2 hours trying to figure out what was going on. Forgotten I’d changed my mail wrapper from send to queue.

    #4 Connor
  5. hi, great post came across your post and jumped off my seat. been pondering on whats wrong with my code, am doing an eloquent query and passing the results to the $data var. the problem is i cant access the date element there when i try to iterate through the array .. which i need to do to send out emails with unique message content… i cant seem to find a solution… please help these are the links to the code and the problem in detail…

    http://laravel.io/forum/12-06-2014-how-do-i-get-array-elements-displayed-in-a-laravel-email-view-urgent

    #5 derrick
  6. This was driving me nuts.
    Thank you for this. It saved my bacon.

    #6 FuturShoc
  7. Thank you man, it works

    #7 Hicham
  8. Thanks a lot

    #8 deebo

Leave a comment