Home » Php » php – ErrorException (E_ERROR) Trying to get property of non-object while trying to access column on relationship table

php – ErrorException (E_ERROR) Trying to get property of non-object while trying to access column on relationship table

Posted by: admin February 25, 2020 Leave a comment

Questions:

I keep getting the error

ErrorException (E_ERROR)

Trying to get property of non-object (View: C:\xampp\schooller\resources\views\teachers\view.blade.php)

And i dont know where the error is coming from each i try to access the

{{ $teacher->class->class }}</td>

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.

Here is my Teacher.php

    class Teacher extends Model
{
    protected $fillable = [
        'full_name', 'photo', 'class', 'credential'
    ];

    public function classes(){
        return $this->belongsTo('App\Classes', 'class');
    }

    public function student(){
        return $this->hasMany('App\Student');
    }
    public function subjects(){
        return $this->belongsToMany('App\Subjects');
    }
}

And, my migration table

    <?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->bigIncrements('id');
            $table->string('full_name')->nullable();
            $table->string('subjects')->nullable();
            $table->string('class')->nullable();
            $table->string('slug')->nullable();
            $table->string('credential')->default('BSC');
            $table->string('photo')->nullable();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('teachers');
    }
}

So, here is the form which I used to add the class->id to the teacher’s table. Works perfectly.

<div class="form-group">
        <label for="class">Select Class</label>
        <select name="class" id="class" class="form-control">
        @foreach($classes as $class)
        <option value="{{ $class->id }}">{{ $class->class }}</option>
        @endforeach
        </select>

So, basically, I have a class table and a teachers table.

I also have a teacher model which returns classes in a belongsTo relationship.

Also, I was able to add the id of the class to a column on the teacher’s table.

Now I’m trying to access the class taught by a teacher by calling $teacher-class-class

And, it keeps throwing a trying to get property of non object.

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.

PS: the controller simply returns the view at the top.

How to&Answers:

your relationship name is classes and you call them with $teacher->class->class should it be like this? $teacher->classes->class