我正在尝试使用Laravel4中的Mail类,但我无法将变量传递给$m对象。
$team对象包含我用eloquent从DB抓取的数据。
Mail::send('emails.report', $data, function($m)
{
$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.com', 'Sender');
});
由于某些原因,我得到了一个错误,其中$team对象不可用。我想这与作用域有关。
有什么想法吗?
发布于 2013-01-24 00:59:58
如果在函数外部实例化了$team
变量,那么它就不在函数作用域中。使用use关键字。
$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.com', 'Sender');
});
注意:正在使用的函数是一个,它不是Laravel所独有的。
https://stackoverflow.com/questions/14482102
复制相似问题