你好,我在Laravel 5.4中工作。我用json_encode将购物车保存到数据库中,这没有问题。我的问题是在我的视图中获取我想要的数据。例如,我想显示产品的名称和价格。
我试过了:
@foreach ($order->cart->items as $item)
{{ dd(json_decode($order->cart, true)) }}
{{ $item['price']['title'] }}
@endforeach这就是我得到下一个错误的时候:
Trying to get property of non-object当我扔掉我的手推车
dd(json_decode($order->cart, true))我得到了:
array:3 [▼
"items" => array:1 [▼
5 => array:4 [▼
"id" => 5
"qty" => 1
"price" => 15
"item" => array:8 [▼
"id" => 5
"created_at" => "2017-06-05 13:21:45"
"updated_at" => "2017-06-05 13:21:45"
"imagePath" => "https://someImage"
"title" => "Vitamin B-12"
"discription" => "Vitamin B-12, also called cobalamin ▶"
"category" => 4
"price" => 15
]
]
]
"totalQty" => 1
"totalPrice" => 15
]发布于 2017-06-13 21:44:06
您的$order->cart是一个数组,而不是一个对象。试试这个:
<?php $data = json_decode($order->cart, true); ?>
@foreach ($data['items'] as $item)
{{ $item['item']['title'] }} {{ $item['item']['price'] }}
@endforeachhttps://stackoverflow.com/questions/44522924
复制相似问题