[英]How to output a Json Array and loop through variables
我的网站上有一个小型票务应用程序,用户可以在其中购买票证并添加客人。 我正在发送回执电子邮件,需要显示来宾姓名。
请注意,最多可容纳7位客人。
来宾在数组中。 如何在电子邮件中正确显示邀请对象?
这是我正在使用的。
电子邮件:
@php
$array = json_encode($guests);
@endphp
@component('mail::message')
# Greetings,...
## Here is an outline of your transaction:
@component('mail::panel')
...
- Guests: @if(!empty($array) && isset($array) && is_array($array))
@foreach ($array as $item)
{{ $item->guest_name }} | {{ $item->gluten }}
@endforeach
@endif
@endcomponent
顶部的数组(包装在PHP标记中)输出以下数据:
[{"no":1,"guest_name":"John Doe","gluten_free":"Yes"}]
我目前没有任何错误可以调试,但是我也没有在电子邮件中输出任何内容。
我正在使用Laravel,所以这是我的邮件文件:我也尝试在此处编写逻辑,但结果相同。
<?php
namespace App\Mail;
use ...
class NewOrder extends Mailable
{
use Queueable, SerializesModels;
public ...
public $guests;
public function __construct(
$...
$guests
)
{
$...
$this->guests = $guests;
// $this->guests = json_encode($guests);
}
public function build()
{
return $this->subject('Order Email')->markdown('emails.user.newOrderEmail');
}
}
任何帮助,将不胜感激。
您需要将所有元素推送到一个主数组中,然后需要将其传递给mail函数。
$details = array();
- Guests: @if(!empty($array) && isset($array) && is_array($array))
@foreach ($array as $item)
//{{ $item->guest_name }} | {{ $item->gluten }}
$current_user = array('name'=>$item->guest_name,'gluten'=>$item->gluten);
array_push($details,$current_user);
@endforeach
现在将$details
数组传递给邮件,我不知道该怎么做,但这是如何传递多个值并将其存储在数组中并传递该数组的方法。
邮件
<?php
namespace App\Mail;
use ...
class NewOrder extends Mailable
{
use Queueable, SerializesModels;
public ...
public $guests;
public function __construct(
$...
$guests
)
{
$...
$this->guests = $guests;
// $this->guests = json_encode($guests);
}
public function build()
{
return $this->subject('Order Email')
->markdown('emails.user.newOrderEmail')
->with([
'ticket_id' => $this->guests['ticket_id'],
....
]);
}
}
降价促销
{{$ticket_id}}
如果您对变量进行json_encode,它将成为字符串。 您应该能够直接循环$guests
数组(假设它已经是数组或集合,而不是其他任何类型)。
可能您没有得到任何输出,因为@if(!empty($array) && isset($array) && is_array($array))
条件为false,因为$array
是字符串,而不是数组。
您可以将代码重写为:
@component('mail::message')
# Greetings,...
## Here is an outline of your transaction:
@component('mail::panel')
...
- Guests:
@unless(empty($guests))
@foreach ($guests as $guest)
{{ $guest['guest_name'] }} | {{ $guest['gluten_free'] }}
@endforeach
@endunless
@endcomponent
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.