title: Why I am getting Trying to get property of non-object in Laravel Eloquent
body: <p>I have the following function:</p>
  
  <pre><code>public function show($id){
  
      $product = Product::findOrFail($id);
      $product_image = $product-&gt;image-&gt;file_name.".".$product-&gt;image-&gt;file_extension;
      return json_encode(array_merge($product-&gt;toArray(), ["image" =&gt; $product_image]));
  
  }
  </code></pre>
  
  <p>Given the id, it will return the following JSON:</p>
  
  <pre><code>{
    "id": 1,
    "parent_id": 0,
    "category_id": 0,
    "image": "1.png"
  }
  </code></pre>
  
  <p>And I have the following function:</p>
  
  <pre><code>public function index() {
  
      return json_encode(Product::all());
  
  }
  </code></pre>
  
  <p>Which will return the following JSON:</p>
  
  <pre><code>[
    {
      "id": 1,
      "parent_id": 0,
      "category_id": 0
    },
    {
      "id": 2,
      "parent_id": 0,
      "category_id": 0
    },
    {
      "id": 3,
      "parent_id": 0,
      "category_id": 0
    },
    {
      "id": 4,
      "parent_id": 0,
      "category_id": 0
    }
  ]
  </code></pre>
  
  <p>My functions are in a Controller class for my API in a Laravel project.</p>
  
  <p><code>ProductsController extends Controller</code></p>
  
  <p><code>Product</code> is a <code>Eloquent</code> model.</p>
  
  <p>What I am trying to achieve is to make index function returns the array with the <code>image</code> property. Here is my try:</p>
  
  <pre><code>public function index() {
  
      $products = Product::all();
  
      $productsWithImages = [];
  
      for($i = 0; $i &lt; count($products); $i++) { 
          $product = $products[$i];
  
          $product_image = $product-&gt;image-&gt;file_name.".".$product-&gt;image-&gt;file_extension;
  
          $productsWithImages[] = array_merge($product-&gt;toArray(), ["image" =&gt; $product_image]);
      }
  
      return json_encode(productsWithImages);
  
  }
  </code></pre>
  
  <p>However, I am getting the following error when I test my API:</p>
  
  <pre><code>Trying to get property of non-object
  </code></pre>
  
  <p>And it points to the following line:</p>
  
  <pre><code>$productsWithImages[] = array_merge($product-&gt;toArray(), ["image" =&gt; $product_image]);
  </code></pre>
  
  <p><code>Product</code> model class:</p>
  
  <pre><code>&lt;?php
  
  namespace App;
  
  use Illuminate\Database\Eloquent\Model;
  use Illuminate\Database\Eloquent\SoftDeletes;
  use App\File;
  
  class Product extends Model
  {
  
      use SoftDeletes;
  
      protected $fillable = ['parent_id', 'category_id'];
  
  
      // this is for voyager
      public function categoryId() {
          return $this-&gt;belongsTo(ProductCategory::class);
      }
  
      // this is for voyager
      public function parentId() {
          return $this-&gt;belongsTo(self::class);
      }
  
      public function category() {
  
          return $this-&gt;belongsTo(ProductCategory::class);
  
      }
  
      public function parent() {
  
          return $this-&gt;belongsTo(Product::class);
  
      }
  
  
      public function files(){
  
          return $this-&gt;hasOne(File::class, 'file_name');
  
      }
  
  
      public function image() {
  
          return $this-&gt;files()-&gt;where('table_name', '=', 'products');
  
      }
  
  }
  </code></pre>
  
  <p>Note: Above this line I have removed some output of Product properties for clarity.</p>
  
  <p>Output of <code>print_r($product)</code>:</p>
  
  <pre><code>App\Product Object
  (
      [dates:protected] =&gt; Array
          (
              [0] =&gt; deleted_at
          )
  
      [fillable:protected] =&gt; Array
          (
              [0] =&gt; name
              [1] =&gt; parent_id
              [2] =&gt; category_id
              [3] =&gt; alternative_name
              [4] =&gt; critical_quantity
              [5] =&gt; allowed_increase
              [6] =&gt; maximum_purchase_quantity
              [7] =&gt; status
              [8] =&gt; sort
          )
  
      [connection:protected] =&gt; mysql
      [table:protected] =&gt; 
      [primaryKey:protected] =&gt; id
      [keyType:protected] =&gt; int
      [incrementing] =&gt; 1
      [with:protected] =&gt; Array
          (
          )
  
      [perPage:protected] =&gt; 15
      [exists] =&gt; 1
      [wasRecentlyCreated] =&gt; 
      [attributes:protected] =&gt; Array
          (
              [id] =&gt; 1
              [parent_id] =&gt; 0
              [category_id] =&gt; 0
              [name] =&gt; Apple
              [alternative_name] =&gt; 
              [critical_quantity] =&gt; 
              [allowed_increase] =&gt; 
              [maximum_purchase_quantity] =&gt; 
              [status] =&gt; 
              [sort] =&gt; 
              [created_at] =&gt; 
              [updated_at] =&gt; 2017-04-23 00:23:46
              [deleted_at] =&gt; 
          )
  
      [original:protected] =&gt; Array
          (
              [id] =&gt; 1
              [parent_id] =&gt; 0
              [category_id] =&gt; 0
              [name] =&gt; Apple
              [alternative_name] =&gt; 
              [critical_quantity] =&gt; 
              [allowed_increase] =&gt; 
              [maximum_purchase_quantity] =&gt; 
              [status] =&gt; 
              [sort] =&gt; 
              [created_at] =&gt; 
              [updated_at] =&gt; 2017-04-23 00:23:46
              [deleted_at] =&gt; 
          )
  
      [casts:protected] =&gt; Array
          (
          )
  
      [dateFormat:protected] =&gt; 
      [appends:protected] =&gt; Array
          (
          )
  
      [events:protected] =&gt; Array
          (
          )
  
      [observables:protected] =&gt; Array
          (
          )
  
      [relations:protected] =&gt; Array
          (
          )
  
      [touches:protected] =&gt; Array
          (
          )
  
      [timestamps] =&gt; 1
      [hidden:protected] =&gt; Array
          (
          )
  
      [visible:protected] =&gt; Array
          (
          )
  
      [guarded:protected] =&gt; Array
          (
              [0] =&gt; *
          )
  
      [forceDeleting:protected] =&gt; 
  )
  </code></pre>