Passing data to a closure in Laravel 4

If you instantiated the $team variable outside of the function, then it’s not in the functions scope. Use the use keyword.

$team = Team::find($id);
Mail::send('emails.report', $data, function($m) use ($team)
{
   $m->to($team->senior->email, $team->senior->first_name . ' '. $team->senior->last_name );
   $m->cc($team->junior->email, $team->junior->first_name . ' '. $team->junior->last_name );
   $m->subject('Monthly Report');
   $m->from('info@website.example', 'Sender');
});

Note: The function being used is a PHP Closure (anonymous function) It is not exclusive to Laravel.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)