title: ErrorException (E_ERROR) Trying to get property of non-object while trying to access column on relationship table
body: <p>I keep getting the error</p>
  
  <pre><code>ErrorException (E_ERROR)
  </code></pre>
  
  <p>Trying to get property of non-object (View: C:\xampp\schooller\resources\views\teachers\view.blade.php)</p>
  
  <p>And i dont know where the error is coming from each i try to access the</p>
  
  <pre><code>{{ $teacher-&gt;class-&gt;class }}&lt;/td&gt;
  </code></pre>
  
  <p>Basically, I'm trying to access a column on the class table, whose id I have added to the teacher's table, linking the two. So, I just want to display the class taught by a teacher, which is something that was already saved In the database. </p>
  
  <p>Here is my Teacher.php</p>
  
  <pre><code>    class Teacher extends Model
  {
      protected $fillable = [
          'full_name', 'photo', 'class', 'credential'
      ];
  
      public function classes(){
          return $this-&gt;belongsTo('App\Classes', 'class');
      }
  
      public function student(){
          return $this-&gt;hasMany('App\Student');
      }
      public function subjects(){
          return $this-&gt;belongsToMany('App\Subjects');
      }
  }
  </code></pre>
  
  <p>And, my migration table</p>
  
  <pre><code>    &lt;?php
  
  use Illuminate\Support\Facades\Schema;
  use Illuminate\Database\Schema\Blueprint;
  use Illuminate\Database\Migrations\Migration;
  
  class CreateTeachersTable extends Migration
  {
      /**
       * Run the migrations.
       *
       * @return void
       */
      public function up()
      {
          Schema::create('teachers', function (Blueprint $table) {
              $table-&gt;bigIncrements('id');
              $table-&gt;string('full_name')-&gt;nullable();
              $table-&gt;string('subjects')-&gt;nullable();
              $table-&gt;string('class')-&gt;nullable();
              $table-&gt;string('slug')-&gt;nullable();
              $table-&gt;string('credential')-&gt;default('BSC');
              $table-&gt;string('photo')-&gt;nullable();
              $table-&gt;timestamps();
          });
      }
  
      /**
       * Reverse the migrations.
       *
       * @return void
       */
      public function down()
      {
          Schema::dropIfExists('teachers');
      }
  }
  </code></pre>
  
  <p>So, here is the form which I used to add the  class->id to the teacher's table. Works perfectly.</p>
  
  <pre><code>&lt;div class="form-group"&gt;
          &lt;label for="class"&gt;Select Class&lt;/label&gt;
          &lt;select name="class" id="class" class="form-control"&gt;
          @foreach($classes as $class)
          &lt;option value="{{ $class-&gt;id }}"&gt;{{ $class-&gt;class }}&lt;/option&gt;
          @endforeach
          &lt;/select&gt;
  </code></pre>
  
  <p>So, basically, I have a class table and a teachers table.</p>
  
  <p>I also have a teacher model which returns classes in a belongsTo relationship. </p>
  
  <p>Also, I was able to add the id of the class to a column on the teacher's table. </p>
  
  <p>Now I'm trying to access the class taught by a teacher by calling $teacher-class-class</p>
  
  <p>And, it keeps throwing a trying to get property of non object.</p>
  
  <p>Could you help me? What am I doing wrong?
  Please I need help and i cant see what i am doing wrong. None of the relationship table property calls are working at all. I hope I was able to explain better.</p>
  
  <p>PS: the controller simply returns the view at the top.</p>