daugaard47
2 months ago
146
16
Laravel

How to output a Json Array and loop through variables

Posted 2 months ago by daugaard47

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":"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 writing the logic in here as well, but same results.

<?php

namespace App\Mail;

use ...

class NewGalaTransaction 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.newGalaTransactionEmail');
    }
}

Any help would be appreciated.

Please sign in or create an account to participate in this conversation.