我有来自移动端的json数据,我需要将其插入两个表orders
和order_details
orders
表包含单个条目,order_details
表包含json object
{
"id": 1,
"user_id": "1",
"delivery_boy_id": "3",
"address_type": "home",
"address": "Address of the customer",
"landmark": "Near Address",
"city": "Mangalore",
"state": "Karnataka",
"pincode": "575002",
"delivery_time_slot": "9am to 11am",
"delivery_date": "23-7-2017",
"promo_code_applied": "no",
"payment_type": "cod",
"transaction_details": null,
"total": "580",
"signature": null,
"status": "delivered",
"comment": null,
"created_at": "2017-06-22 01:05:16",
"updated_at": "2017-06-22 01:05:16",
"order_details": [
{
"id": 1,
"product_id": "1",
"quantity": 2,
"created_at": "2017-06-22 01:05:16",
"updated_at": "2017-06-22 01:05:16"
},
{
"id": 2,
"product_id": "2",
"quantity": 3,
"created_at": "2017-06-22 01:05:16",
"updated_at": "2017-06-22 01:05:16"
}
]
}
我可以将数据保存到orders
表,我想修改(追加保存的order_id)并将order_details
数据保存到order_details
表
public function placeOrder(Request $request)
{
$order = new Order;
$order->outlet_id = $request->outlet_id;
$order->user_id = $request->user_id;
$order->delivery_boy_id = $request->delivery_boy_id;
$order->address_type = $request->address_type;
$order->address = $request->address;
$order->landmark = $request->landmark;
$order->city = $request->city;
$order->state = $request->state;
$order->pincode = $request->pincode;
$order->delivery_time_slot = $request->delivery_time_slot;
$order->delivery_date = $request->delivery_date;
$order->promo_code_applied = $request->promo_code_applied;
$order->payment_type = $request->payment_type;
$order->transaction_details = $request->transaction_details;
$order->total = $request->total;
$order->signature = $request->signature;
$order->status = $request->status;
$order->comment = $request->comment;
$order->save();
//loop through the order_details array from above JSON
//add the above saved order_id to order_details
//save the orers_details array in order_details table
foreach ($request->order_details as $key => $order_detail) {
$details = new OrderDetail;
$details->order_id = $order->id;
$details->product_id = $order_detail->product_id; //line 43
$details->quantity = $order_detail->quantity;
$details->save();
}
return Response::json([
'data' => $order->id
]);
}
我收到以下错误
(1/1) ErrorException
Trying to get property of non-object
in OrderController.php (line 43)
谢谢
答案 0 :(得分:1)
使用
$details->product_id = $order_detail['product_id']
$details->quantity =$order_detail['quantity'];
当您尝试使用 - >访问正常数组值时会发生这种情况。操作