如何发送可邮寄的收藏并在视图中组织我在对象中的收藏中创建的项目?
在我的控制器中,我正在创建一个像这样的订单:
$order = Order::create([
'products' => json_encode(new CartItemCollection($items)),
'price' => $PartialPrice,
//other stuff
]);
在我的购买功能中,我正在发送如下电子邮件:
$pro = $order->products;
$data = [
'extra_address' => $order->extra_address,
'address' => $order->address,
];
Mail::to($check_user->email)->send(new NewPurch($data, $pro));
在我的电子邮件配置中,我有以下内容:
public $data;
public $pro;
public function __construct($data,$pro)
{
$this->data = $data;
$this->pro = $pro;
}
public function build()
{
return $this->markdown('newpurch')->with(['data' => $this->data])->subject('Thank you');
}
在视图中,我收到了数据,但是我不知道如何访问每个数据
@component('mail::message')
Someone want this:
{{$pro}}
@endcomponent
但是我不能作为对象$pro->id
或作为键$pro['id']
编辑: 如果我尝试这样做:
@component('mail::message')
Someone want this:
@foreach($pro->items as $item)
{{ $item->id }}
@endforeach
@endcomponent
出现此错误:
试图获取非对象的属性“项目”(查看:/app/resources/views/newpurch.blade.php)
如果我尝试这样做:
@component('mail::message')
<?php
$deco = json_decode($pro,true);
?>
Someone want this:
{{ $deco }}
@endcomponent
出现此错误:
htmlspecialchars()期望参数1为字符串,给定数组(查看:/app/resources/views/newpurch.blade.php)
答案 0 :(得分:2)
编辑:
public $data;
public $pro;
public function __construct($data, $pro)
{
$this->data = $data;
$this->pro = json_decode($pro, true);
}
public function build()
{
return $this->markdown('newpurch')->with(['data' => $this->data])->subject('Thank you');
}
并使用foreach,因为我们现在返回集合而不是字符串。
@foreach($pro->items as $item)
{{ $item->id }}
@endforeach