如何将集合传递给可邮寄的laravel

问题描述 投票:1回答:1

如何发送可邮寄的集合并在视图中组织在对象中的集合中创建的项目?

在我的控制器中,我正在创建这样的订单:

$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

电子邮件看起来像:enter image description here

但是我不能作为对象$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)

php laravel email
1个回答
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

© www.soinside.com 2019 - 2024. All rights reserved.