这是我的查询
$productItems = ProductItemResource::collection(ProductItem::where('pd_id', $id)->get());
查询的输出是这样的:
$output = [[
'id' => 1,
'serial' => "XXXXXXAA1",
'pd_item_info' =>[
'id' => 1,
'quantity' => 5,
'product_info' => [
'id' => 1,
'product_name' => 'Keyboard'
],
]
],[
'id' => 2,
'serial' => "XXXXXXAA2",
'pd_item_info' =>[
'id' => 2,
'quantity' => 10,
'product_info' => [
'id' => 2,
'product_name' => 'Monitor'
],
]
]];
这是我的病情:
foreach ($output as $productItem) {
return $productItem->pd_item_info->product_info['product_name'];
// IT HAS AN ERROR WHERE I CAN'T ACCESS THE OBJECT OF OBJECT
}
为什么在使用资源时访问对象时出错?
答案 0 :(得分:0)
结果是数组不是对象。
foreach($output as productItem) {
return $productItem['pd_item_info']['product_info']['product_name'];
}
答案 1 :(得分:0)
为什么要创建一个新集合?
$productItems = ProductItemResource::collection(ProductItem::where('pd_id', $id)->get());
查询结果已经是集合
ProductItem::where('pd_id', $id)->get()