jeudi 1 octobre 2020

Poplating invoice items with cart data - Laravel - Invalid argument supplied for foreach()

Trying to display the list of Items in the cart after the Order is successfull. Unable to populate the Cart Items(cart_items) while generating the Invoice.

Cart Data before being saved to database,

dd($request['cart_items']);
        $cart =  Cart::updateOrCreate([
            'user_id' => $user_id,
            'cart_items' =>  $request['cart_items'],
        ]);

[{"productName":"demo product","productPrice":"1600","productId":"1","productImage":"http://localhost/EcommerceBackend/public//assets/images/product/thumb/books.jpg","productQuantity":2},{"productName":"demo product","productPrice":"1600","productId":"1","productImage":"http://localhost/EcommerceBackend/public//assets/images/product/thumb/books.jpg","productQuantity":2}]

In my Invoice Blade

{
   "id":1,
   "orderId":"ORDER--1",
   "orderAmount":"2400.00",
   "created_at":"2020-09-23T02:29:44.000000Z",
   "updated_at":"2020-09-23T02:29:44.000000Z",
   "ecommerce_prepayment_orders":{
      "id":1,
      "user_id":1208268476,
      "cart_id":2,
      "order_name":"ORDER--1",
      "billing_name":"asdasdfa",
      "billing_last_name":"asdfasdfasdf",
      "address":"asdfasdf",
      "pancard":"",
      "gstin":"",
      "deleted_at":null,
      "created_at":"2020-09-23T02:29:16.000000Z",
      "updated_at":"2020-09-23T02:29:16.000000Z",
      "cart":{
         "id":2,
         "user_id":1208268476,
         "cart_items":"[{\"productName\":\"Product Name\",\"productPrice\":\"1200\",\"productId\":\"1\",\"productImage\":\"http:\/\/localhost\/EcommerceBackend\/public\/\/assets\/images\/product\/thumb\/Product Name.jpeg\",\"productQuantity\":2},{\"productName\":\"Product Name\",\"productPrice\":\"1200\",\"productId\":\"1\",\"productImage\":\"http:\/\/localhost\/EcommerceBackend\/public\/\/assets\/images\/product\/thumb\/Product Name.jpeg\",\"productQuantity\":2}]",
         "deleted_at":null,
         "created_at":"2020-09-23T02:29:16.000000Z",
         "updated_at":"2020-09-23T02:29:16.000000Z"
      }
   }
}

I was able to populate the other data except the cart_items.

When I try to loop the foreach, it returns Invalid argument supplied for foreach()

  @foreach ($invoice_items['ecommercePrepaymentOrders']['cart']->cart_items as $item)
    
    @endforeach

What I am doing wrong.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire