【发布时间】:2020-11-06 23:21:47
【问题描述】:
我有 json 列,我在其中保存订单数据,下面的代码是该数据的示例:
[{"id":27,"name":"new product","price":7246,"quantity":"1","attributes":[],"conditions":[]}]
如果我的编辑页面中有这样的循环,我可以使用上面的代码获取订单信息:
@foreach($order->product_data as $data)
{{ $data['quantity'] }}
@endforeach
直到这里一切都很好,但是当我的订单具有属性 "attributes":[], 时出现问题,然后我收到此错误:
为 foreach() 提供的参数无效
我在 json 列中的数据是这样的:
"[{\"id\":29,\"name\":\"effewf\",\"price\":24524,\"quantity\":\"1\",\"attributes\":[{\"attr\":{\"label\":\"Red\",\"price\":\"5000.00\"}},{\"attr\":{\"label\":\"22\\\"\",\"price\":\"900000.00\"}}],\"conditions\":[]}]"
如您所见,此订单有 2 个属性。 (可多可少)。
附加
这就是我在订单模型中获取这些数据的方式:
protected $casts = [
'product_data' => 'array',
];
知道我为什么会收到该错误,以及无论我的订单是否有属性,我如何获取我的属性数据?
更新
如果我打开没有属性和dd 的订单编辑页面,结果将是:
array:6 [▼
"id" => 27
"name" => "new product"
"price" => 7246
"quantity" => "1"
"attributes" => []
"conditions" => []
]
如果在带有属性的订单上加载相同的内容,则会出现上述错误。
【问题讨论】:
-
你能告诉我们你对属性做了什么吗??
-
什么意思?你需要看什么?这样我就可以给你了
-
您遇到错误
"attributes":[]的原因,您对此做了什么,显示给出此错误的代码 -
@Sohel0415 我什至一个字都没做!基于我的循环(我分享的问题)直接给出了这个错误。
-
在你看来如何访问
attributes[]?