Laravel 5.1,是否可以将集合发送到Mail模板

时间:2022-10-25 21:35:59
Mail::send('emails.mytemplate', $mymodel, function ($message) use ($email) {
      $message->from('info@mail.com', 'Info')
              ->sender('info@mail.com', 'Info')
              ->replyTo(env('MAIL_REPLY_TO', 'info@mail.com'), 'Info')
              ->to($email)
              ->subject('You have new mail');
});

gives

Argument 2 passed to Illuminate\Mail\Mailer::send() must be of the type array, object given....

传递给Illuminate \ Mail \ Mailer :: send()的参数2必须是类型数组,对象给出....

since the data I want to send is somewhat complex I would really like to send $myModel collection to the template and pull the data from the relations of MyModel in there instead of parsing all the data to new multilevel array and passing that to the Mail::send(...);

因为我想要发送的数据有点复杂,我真的想将$ myModel集合发送到模板并从那里的MyModel关系中提取数据,而不是将所有数据解析为新的多级数组并将其传递给Mail: :发送(...);

Is that the right way to go or should I just parse the model and all of its relations to a new array and go with that?

这是正确的方法还是我应该将模型及其所有关系解析为一个新的数组并继续使用它?

1 个解决方案

#1


1  

There is a way to send an actual object to the mail template. Simply change your logic to:

有一种方法可以将实际对象发送到邮件模板。只需将您的逻辑更改为:

Mail::send('emails.mytemplate', ["mymodel" => $mymodel], function ($message) use ($email) {
      $message->from('info@mail.com', 'Info')
              ->sender('info@mail.com', 'Info')
              ->replyTo(env('MAIL_REPLY_TO', 'info@mail.com'), 'Info')
              ->to($email)
              ->subject('You have new mail');
});

That way, in your emails.mytemplate.blade.php you can still access data off of that object by using:

这样,在您的emails.mytemplate.blade.php中,您仍然可以使用以下方法访问该对象的数据:

<p>My Model's name is {{ $mymodel->name }}</p>

#1


1  

There is a way to send an actual object to the mail template. Simply change your logic to:

有一种方法可以将实际对象发送到邮件模板。只需将您的逻辑更改为:

Mail::send('emails.mytemplate', ["mymodel" => $mymodel], function ($message) use ($email) {
      $message->from('info@mail.com', 'Info')
              ->sender('info@mail.com', 'Info')
              ->replyTo(env('MAIL_REPLY_TO', 'info@mail.com'), 'Info')
              ->to($email)
              ->subject('You have new mail');
});

That way, in your emails.mytemplate.blade.php you can still access data off of that object by using:

这样,在您的emails.mytemplate.blade.php中,您仍然可以使用以下方法访问该对象的数据:

<p>My Model's name is {{ $mymodel->name }}</p>