Dexx
Dexx

Reputation: 163

Laravel Array to String Issue

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"
]
]

Upvotes: 0

Views: 105

Answers (1)

DevK
DevK

Reputation: 9962

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;
}

Upvotes: 2

Related Questions