0
votes

I'm using
"bumbummen99/shoppingcart": "^2.8",
In laravel 5.8
Help me to send my cart::content to the blade template.

//ShoppingController.php
Mail::send('emails.ordermail-customer-response',
       array(
            'cartcontent' => Cart::content()
), function($message) use ($request)

When I'm sending Cart::content to the e-mail blade template, everything works great

 @foreach ($cartcontent as $item)
 
 {{$item->name}}
 
 {{$item->price}}
 
 @endforeach

But when I'm saving cart content to my database like this

///shoppingcontroller.php

$order = Orders::create([

            'cartcontent' => Cart::content()
 ]);
In my database I have a string

{"76074528274137cc4af420551cfac36a":{"rowId":"76074528274137cc4af420551cfac36a",
"id":9,"name":"product","qty":"1",
"price":382,"weight":0,"options":{"color":"black","variant":null,"deliveryprice":"40",
"qtyinOnePocket":"1"},
"discount":0,"tax":0,"subtotal":382}}

Than I' trying to send the data from database to my blade template

//shoppingcontroller.php

public function showSingleOrder($orderId)
    {

        $order = Orders::find($orderId);

        return view('admin.sections.orders.ordersingle')
        ->with('order', $order);

    }

And here is my blade

////ordersingle.blade.php

{{$order->cartcontent}}

Gives me this

{"76074528274137cc4af420551cfac36a":{"rowId":"76074528274137cc4af420551cfac36a",
"id":9,"name":"product","qty":"1",
"price":382,"weight":0,"options":{"color":"black","variant":null,"deliveryprice":"40",
"qtyinOnePocket":"1"},
"discount":0,"tax":0,"subtotal":382}}

But when I'm using foreach in my blade like this

//ordersingle.blade

@foreach($order->cartcontent as $item)
      {{$item->price}}
@endforeach

I don't recieve the price but an error

Invalid argument supplied for foreach()
1
$order->cartcontent is a JSON string; you can't use it in a @foreach() loop without converting it to something that PHP can iterate over. - Tim Lewis
check this - Amr Aly

1 Answers

1
votes

Add casts to your orders model. It'll transform the JSON saved in the cartcontent column back into an array using json_decode().

Orders.php

class Orders extends Model {
    // ...

    protected $casts = [
        'cartcontent' => 'array',
    ];

    // ...
}