title: Laravel Relationship: Trying to get property 'description' of non-object
body: <p>I'm facing against this problem since yesterday.
  I have a db table called <code>resources</code> have a foreign key linked to another table called <code>category</code>.</p>
  <p><img src="https://i.stack.imgur.com/h1zsw.png" alt="Screenshot of database tables" /></p>
  <p>I'm trying to retrieve the <code>description</code> field in my blade view, but I get this error:</p>
  <blockquote>
  <p>Trying to get property 'description' of non-object.</p>
  </blockquote>
  <p>My blade view:</p>
  <pre class="lang-php prettyprint-override"><code>@extends('templates.header')
  
  @section('section')
      &lt;div class=&quot;p-10 grid grid-cols-1 sm:grid-cols-1 md:grid-cols-3 lg:grid-cols-3 xl:grid-cols-3 gap-5&quot;&gt;
          @foreach($resources as $resource)
              &lt;div class=&quot;max-w-sm rounded overflow-hidden shadow-lg&quot;&gt;
                  {{-- &lt;img class=&quot;w-full&quot; src=&quot;#&quot; alt=&quot;Mountain&quot;&gt; --}}
                  &lt;div class=&quot;px-6 py-4&quot;&gt;
                      &lt;div class=&quot;font-bold text-xl mb-2&quot;&gt;
                          {{ $resource-&gt;name }}
                      &lt;/div&gt;
                      &lt;p class=&quot;text-gray-700 text-base&quot;&gt;
                          {{ $resource-&gt;description }}
                      &lt;/p&gt;
                  &lt;/div&gt;
                  &lt;div class=&quot;px-6 pt-4 pb-2&quot;&gt;
                      &lt;span class=&quot;inline-block bg-gray-200 rounded-full px-3 py-1 text-sm font-semibold text-gray-700 mr-2 mb-2&quot;&gt;{{ $resource-&gt;categor-&gt;description }}&lt;/span&gt;
                      &lt;span class=&quot;inline-block bg-gray-200 rounded-full px-3 py-1 text-sm font-semibold text-gray-700 mr-2 mb-2&quot;&gt;{{ $resource-&gt;status }}&lt;/span&gt;
                      &lt;span class=&quot;inline-block bg-gray-200 rounded-full px-3 py-1 text-sm font-semibold text-gray-700 mr-2 mb-2&quot;&gt;{{ $resource-&gt;centerId }}&lt;/span&gt;
                      &lt;button type=&quot;submit&quot; class=&quot;inline-flex justify-center py-2 px-4 border border-transparent shadow-sm text-sm font-medium rounded-md text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500&quot;&gt;
                          Prenota
                      &lt;/button&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          @endforeach
      &lt;/div&gt;
  @endsection
  </code></pre>
  <p>My Resource Model:</p>
  <pre class="lang-php prettyprint-override"><code>namespace App\Models;
  
  use App\Models\Category;
  use Illuminate\Database\Eloquent\Factories\HasFactory;
  use Illuminate\Database\Eloquent\Model;
  
  class Resource extends Model
  {
      use HasFactory;
  
      /**
       * The attributes that are mass assignable.
       *
       * @var array
       */
      protected $fillable = [
          'name',
          'description',
          'category',
          'inventoryN',
          'status',
          'centerId',
      ];
  
      /**
       * The attributes that should be hidden for arrays.
       *
       * @var array
       */
      protected $hidden = [
      ];
  
      /**
       * The attributes that should be cast to native types.
       *
       * @var array
       */
      protected $casts = [
      ];
  
      public function category()
      {
          return $this-&gt;hasOne(Category::class, 'id', 'category');
      }
  }
  </code></pre>
  <p>My Category Model:</p>
  <pre class="lang-php prettyprint-override"><code>namespace App\Models;
  
  use App\Models\Resource;
  use Illuminate\Database\Eloquent\Factories\HasFactory;
  use Illuminate\Database\Eloquent\Model;
  
  class Category extends Model
  {
      use HasFactory;
  
      protected $table = 'categories';
  
      /**
       * The attributes that are mass assignable.
       *
       * @var array
       */
      protected $fillable = [
          'description',
      ];
  
      /**
      * The attributes that should be hidden for arrays.
      *
      * @var array
      */
      protected $hidden = [
      ];
  
      /**
      * The attributes that should be cast to native types.
      *
      * @var array
      */
      protected $casts = [
      ];
  
      public function resource()
      {
          return $this-&gt;belongsTo(Resource::class, 'category');
      }
  }
  
  </code></pre>
  <p>And lastly my ResourceController:</p>
  <pre class="lang-php prettyprint-override"><code>namespace App\Http\Controllers;
  
  use App\Models\Category;
  use App\Models\Resource;
  use Illuminate\Http\Request;
  
  class ResourceController extends Controller
  {
      public function index()
      {
          $resources = Resource::with('category')-&gt;get();
  
          return view('resources', compact('resources'));
      }
  }
  </code></pre>
  <p>This is a dd of &quot;$resources&quot;:
  <a href="https://i.stack.imgur.com/YRnEc.png" rel="nofollow noreferrer">dd of $resources</a></p>