title: How to access a object field in laravel?
body: <p>I use laravel framework. I can retrieve data from database with relationship with this code:</p>
  
  <pre><code>$orders = Order::with('orderType', 'orderStatus', 'user')-&gt;get();
  </code></pre>
  
  <p>The result of the above command is:</p>
  
  <pre><code>[
  {
    "id":16,
    "shop_id":1,
    "user_id":0,
    "delivery_cost":null,
    "address":null,
    "latitude":null,
    "longitude":null,
    "total_price":655000,
    "total_price_off":551000,
    "order_status_id":5,
    "order_type_id":3,
    "reserve_id":null,
    "created_at":"1399-02-01 13:25:51",
    "order_type":{
      "id":3,
      "name":"admin"
    },
    "order_status":{
      "id":5,
      "name":"system"
    },
    "user":null
  }
  ]
  </code></pre>
  
  <p>My php code is:</p>
  
  <pre><code>foreach($orders as $order){
     echo $order-&gt;order_status_name; \\ I get error for this line;
  }
  </code></pre>
  
  <p>Now my problem is how can i access the name field in order_status? when i want access to total price i write this code </p>
  
  <pre><code>$order-&gt;total_price
  </code></pre>
  
  <p>and get the total price without any error, but when is write this code</p>
  
  <pre><code>$order-&gt;order_status-&gt;name
  </code></pre>
  
  <p>i get this error:</p>
  
  <pre><code>Trying to get property 'name' of non-object
  </code></pre>