Ben*_*lez 101 php laravel laravel-4
我正在尝试使用Laravel 4中的Mail Class,而我无法将变量传递给$ 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('info@website.com', 'Sender');
});
Run Code Online (Sandbox Code Playgroud)
出于某种原因,我得到一个错误,其中$ team对象不可用.我想它与范围有关.
有任何想法吗 ?
Ble*_*ing 224
如果$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');
});
Run Code Online (Sandbox Code Playgroud)
注意:正在使用的函数是PHP Closure(匿名函数)它不是Laravel独有的.