我正在尝试使用 Laravel 4 中的 Mail 类,但无法将变量传递给 $m 对象。
$team 对象包含我用 eloquent 从数据库中获取的数据。
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('[email protected] /cdn-cgi/l/email-protection', 'Sender');
});
由于某种原因,我收到 $team 对象不可用的错误。我想这和范围有关系。
如果您实例化了$team
变量在函数之外,则它不在函数范围内。使用use https://secure.php.net/manual/en/language.namespaces.importing.php关键词。
$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('[email protected] /cdn-cgi/l/email-protection', 'Sender');
});
注意:正在使用的函数是PHP 闭包(匿名函数) http://php.net/manual/en/class.closure.php它并不是 Laravel 独有的。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)