简体   繁体   中英

How to output a Json Array and loop through variables

I have a small ticket app on my site where users can buy tickets and add guests. I'm sending out the Receipt Email and need to display the guest names.

Note, There can be up to 7 guests.

The Guest are in an array. How can I display the guests properly in the email?

Here is what I'm working with.

Email:

@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

The array at the top, wrapped in the PHP Tags outputs the following data:

[{"no":1,"guest_name":"John Doe","gluten_free":"Yes"}]

I'm currently not getting any errors to debug, but I'm not getting anything outputted in the email either.

I'm using Laravel so here is my Mail File: I tried writting the logic in here as well, but same results.

<?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');
    }
}

Any help would be appreciated.

You need to push all the elements into one master array and then need to pass it mail function.

$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

Now pass the $details array to mail, I dont know how you do it, but this is how you can pass multiple values store it in array and pass that array.

Mail

<?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'],
                ....
            ]);
    }
}

Markdown

{{$ticket_id}}

If you do json_encode the variable it would become a string. You should be able to directly loop over $guests array (assuming it is already an array or a collection, not any other type).
Probably you aren't getting any output as @if(!empty($array) && isset($array) && is_array($array)) condition is false, as $array is a string, not an array.

You could rewrite your code as:

@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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM