Laravel Mail为什么数据库中的数组变量不起作用?

taor4pac  于 6个月前  发布在  其他
关注(0)|答案(1)|浏览(48)

如何在Laravel中将数据库中的数组添加到我的电子邮件组件中?
我的控制器:

$request = DB::table('Requests')->where('id', $id)->get();
$email = DB::table('Requests')->where('id', $id)->value('email');

 Mail::to($email)->send(new Order($request));

字符串
我的电子邮件组件订单:

Dear {{ $request->name }}


我读到过用compact('request')函数添加数组,但这也不起作用。
谢谢你的帮助!

erhoui1w

erhoui1w1#

您当前使用get()的调用将返回一个集合,这可能是您对数组的混淆。
要在mailable中引用它,您需要集合的第一个元素(假设它被找到)。

Dear {{ $request[0]->name }}

字符串
也许可以尝试使用first()来获取单个资源。
例如

$request = DB::table('Requests')->where('id', $id)->get()->first();
// or use eloquent
$request = Requests::where('id', $id)->firstOrFail();
// or if id is the primaray key
$request = Requests::find('id', $id);


所有这些都将为您提供一个单一的资源。
然后,您可以将其传递到您的mailable中,

Mail::to($email)->send(new Order($request));


现在你正在传递一个模型对象,你可以像在邮件中那样引用它。

Dear {{ $request->name }}

相关问题