title: Laravel - Insert JSON Array Data to Database
body: <p>I have json data coming from mobile end which i need to insert into two tables <code>orders</code> and <code>order_details</code></p>
  
  <p>The <code>orders</code> table have single entry and the <code>order_details</code> table have array of <code>json object</code> </p>
  
  <pre><code>{
      "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"
          }
      ]
  }
  </code></pre>
  
  <p>I am able to save the data to <code>orders</code> table, and i want to modify(append saved order_id) and save the <code>order_details</code> data to <code>order_details</code> table</p>
  
  <pre><code>public function placeOrder(Request $request)
  {   
      $order = new Order;
  
      $order-&gt;outlet_id = $request-&gt;outlet_id;
      $order-&gt;user_id = $request-&gt;user_id;
      $order-&gt;delivery_boy_id = $request-&gt;delivery_boy_id;
      $order-&gt;address_type = $request-&gt;address_type;
      $order-&gt;address = $request-&gt;address;
      $order-&gt;landmark = $request-&gt;landmark;
      $order-&gt;city = $request-&gt;city;
      $order-&gt;state = $request-&gt;state;
      $order-&gt;pincode = $request-&gt;pincode;
      $order-&gt;delivery_time_slot = $request-&gt;delivery_time_slot;
      $order-&gt;delivery_date = $request-&gt;delivery_date;
      $order-&gt;promo_code_applied = $request-&gt;promo_code_applied;
      $order-&gt;payment_type = $request-&gt;payment_type;
      $order-&gt;transaction_details = $request-&gt;transaction_details;
      $order-&gt;total = $request-&gt;total;
      $order-&gt;signature = $request-&gt;signature;
      $order-&gt;status = $request-&gt;status;
      $order-&gt;comment = $request-&gt;comment;
      $order-&gt;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-&gt;order_details as $key =&gt; $order_detail) {
  
          $details = new OrderDetail;
  
          $details-&gt;order_id = $order-&gt;id;
          $details-&gt;product_id = $order_detail-&gt;product_id; //line 43
          $details-&gt;quantity = $order_detail-&gt;quantity;
          $details-&gt;save();
  
      }
  
      return Response::json([
          'data' =&gt; $order-&gt;id
      ]);
  }
  </code></pre>
  
  <p>i am getting the following error</p>
  
  <pre><code>(1/1) ErrorException
  Trying to get property of non-object
  in OrderController.php (line 43)
  </code></pre>
  
  <p>thank you</p>