0

I have the following array inside of my EnrolmentDetailsController which I can't seem to loop through and display properly inside of an email view:

        $data = array(
         'student_first_name'    => $customer->student_first_name,
         'student_last_name'     => $customer->student_last_name,
         'student_email'         => $customer->student_email,
         'subject'               => 'Confirmation of Payment Plan',
        );

        foreach($customer->orders as $order){
        $data['product_descriptions'][] = array(
        'product_description' => $order->product_description,
        );
        }



    Mail::send('emails.confirmation', $data, function($confirmation) use ($data){
        $confirmation->from('[email protected]');
        $confirmation->to($data['student_email']);
        $confirmation->subject($data['subject']);
    }); 

If I use the following in my view it will only show the first value in the product_descriptions array with the other $data array information:

<?php foreach($product_descriptions as $item){echo $item;} ?>

If I specifiy the key I can get each entry in the array but is not ideal as it throws errors if the product orders don't match the amount of offsets in the email view etc:

<?php foreach($product_descriptions[0] as $item){echo $item;} ?>

If I simply use the following in my email view I get array to string error: {{$product_descriptions}}

dd($product_descriptions) gives me the following array in my email view

array:2 [▼
0 => array:1 [▼
"product_description" => "Course 1"
]
1 => array:1 [▼
"product_description" => "Course 2"
]
]

1 Answer 1

2

You're wrapping it in one array to many here:

foreach($customer->orders as $order){
    $data['product_descriptions'][] = array(
        'product_description' => $order->product_description,
    );
}

Remove the surrounding array(...) and it should be fine. Like this:

foreach($customer->orders as $order){
    $data['product_descriptions'][] = order->product_description;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Cheers! I did try that but must have had the syntax wrong earlier, legend.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.