0

I have json data coming from mobile end which i need to insert into two tables orders and order_details

The orders table have single entry and the order_details table have array of 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"
        }
    ]
}

I am able to save the data to orders table, and i want to modify(append saved order_id) and save the order_details data to order_details table

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
    ]);
}

i am getting the following error

(1/1) ErrorException
Trying to get property of non-object
in OrderController.php (line 43)

thank you

5
  • Which line is 43? Commented Jun 27, 2017 at 15:19
  • @Alex Howansky thank you for your time, i have mentioned it in the code $details->product_id = $order_detail->product_id; //line 43 Commented Jun 27, 2017 at 15:21
  • Ah ok, missed that... Commented Jun 27, 2017 at 15:22
  • Have you check that $order_detail is an object? Commented Jun 27, 2017 at 15:24
  • This works ok for me with the JSON you've posted. I suspect that in your case, you're getting JSON with an empty array for the order_details key. Commented Jun 27, 2017 at 15:27

1 Answer 1

2

use

 $details->product_id = $order_detail['product_id']
 $details->quantity =$order_detail['quantity'];

this happens when you are trying to access normal array values using -> operator.

Sign up to request clarification or add additional context in comments.

3 Comments

thank you so much for your time, really appreciate it, it worked
i would like to know why using ->operator we can't access the value
@MrRobot Because $order_detail is an array and not an object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.