我有一个DB表,它存储id、cart(数组)、updated_at、created_at。如何解构数组并将视图中的项显示为列表项。
3.
{“3”:{“名称”:“iPhone”,“数量”:2,“价格”:“500.00”},"4":{“名称”:“三星”,“数量”:1,“价格”:“344.00”}
2019年-11-10 07:50:53
______________________
ProductsController
______________________
public function cartsIndex(Request $request)
{
$carts = Cart::all();
$carts = json_decode($carts);
// return view('cartsIndex')->with(['carts' => $carts]);
dd($carts);
}我希望手推车显示如下。
- Name: Iphone - Quantity: 2
- Price: 500
- Name: Samsung - Quantity: 1
- Price: 344
发布于 2019-11-10 09:17:43
在模型中使用laravel的$cast属性。它将自动帮助您将数据转换为json,同时存储在数据库中,并将帮助您在检索数据时以数组的形式访问它们。
protected $cast = [
'cart' => 'array' // name of the column you want to store as json and retrieve as array
];供参考https://laravel.com/docs/6.x/eloquent-mutators#array-and-json-casting
https://stackoverflow.com/questions/58786883
复制相似问题