Laravel请求有数组但迭代错误"Trying to get property 'product' of non-object"
Laravel Request has array but iteration error "Trying to get property 'product' of non-object"
我用的时候
return $request->all();
网络预览选项卡
0 => array:10 [
"id" => 1
"user_id" => 1
"product_id" => 1
"brand_id" => 1
"quantity" => 2
"total" => "90000"
"created_at" => "2021-05-21T07:48:34.000000Z"
"updated_at" => "2021-05-21T07:48:34.000000Z"
"product" => array:10 [
"id" => 1
"name" => "S1 Pro"
"category_id" => 1
"sub_category_id" => 1
"brand_id" => 1
"buy_price" => "40000.00"
"sell_price" => "45000.00"
"description" => "this is a new phone"
"created_at" => "2021-05-20T06:02:28.000000Z"
"updated_at" => "2021-05-20T06:02:28.000000Z"
]
"brand" => array:4 [
"id" => 1
"name" => "Vevo"
"created_at" => "2021-05-20T05:48:01.000000Z"
"updated_at" => "2021-05-20T05:48:01.000000Z"
]
]
1 => array:10 [
"id" => 2
"user_id" => 1
"product_id" => 2
"brand_id" => 2
"quantity" => 1
"total" => "195000"
"created_at" => "2021-05-21T08:49:47.000000Z"
"updated_at" => "2021-05-21T08:49:47.000000Z"
"product" => array:10 [
"id" => 2
"name" => "Note 20 ultra"
"category_id" => 1
"sub_category_id" => 2
"brand_id" => 2
"buy_price" => "180000.00"
"sell_price" => "195000.00"
"description" => "A new phone a new Era"
"created_at" => "2021-05-21T06:39:43.000000Z"
"updated_at" => "2021-05-21T06:39:43.000000Z"
]
"brand" => array:4 [
"id" => 2
"name" => "Samsung"
"created_at" => "2021-05-21T06:37:54.000000Z"
"updated_at" => "2021-05-21T06:37:54.000000Z"
]
]
]
但是当我使用 foreach 循环访问每个项目时,它给出错误“尝试获取非对象的 属性 'product'”
我正在使用的方法
public function sellProduct(Request $request){
// dd($request->all());
$cardData = $request->all();
foreach ($cardData as $value) {
return $value->product;
}
}
请帮助解决这个问题,基本上我是通过请求创建购物车应用我正在获取所有详细信息和产品
像 $value['product']
一样访问产品,因为它是数组而不是对象。
我用的时候
return $request->all();
网络预览选项卡
0 => array:10 [
"id" => 1
"user_id" => 1
"product_id" => 1
"brand_id" => 1
"quantity" => 2
"total" => "90000"
"created_at" => "2021-05-21T07:48:34.000000Z"
"updated_at" => "2021-05-21T07:48:34.000000Z"
"product" => array:10 [
"id" => 1
"name" => "S1 Pro"
"category_id" => 1
"sub_category_id" => 1
"brand_id" => 1
"buy_price" => "40000.00"
"sell_price" => "45000.00"
"description" => "this is a new phone"
"created_at" => "2021-05-20T06:02:28.000000Z"
"updated_at" => "2021-05-20T06:02:28.000000Z"
]
"brand" => array:4 [
"id" => 1
"name" => "Vevo"
"created_at" => "2021-05-20T05:48:01.000000Z"
"updated_at" => "2021-05-20T05:48:01.000000Z"
]
]
1 => array:10 [
"id" => 2
"user_id" => 1
"product_id" => 2
"brand_id" => 2
"quantity" => 1
"total" => "195000"
"created_at" => "2021-05-21T08:49:47.000000Z"
"updated_at" => "2021-05-21T08:49:47.000000Z"
"product" => array:10 [
"id" => 2
"name" => "Note 20 ultra"
"category_id" => 1
"sub_category_id" => 2
"brand_id" => 2
"buy_price" => "180000.00"
"sell_price" => "195000.00"
"description" => "A new phone a new Era"
"created_at" => "2021-05-21T06:39:43.000000Z"
"updated_at" => "2021-05-21T06:39:43.000000Z"
]
"brand" => array:4 [
"id" => 2
"name" => "Samsung"
"created_at" => "2021-05-21T06:37:54.000000Z"
"updated_at" => "2021-05-21T06:37:54.000000Z"
]
]
]
但是当我使用 foreach 循环访问每个项目时,它给出错误“尝试获取非对象的 属性 'product'”
我正在使用的方法
public function sellProduct(Request $request){
// dd($request->all());
$cardData = $request->all();
foreach ($cardData as $value) {
return $value->product;
}
}
请帮助解决这个问题,基本上我是通过请求创建购物车应用我正在获取所有详细信息和产品
像 $value['product']
一样访问产品,因为它是数组而不是对象。