Trying to get property of non-object

Posted 10 months ago by irankhosravi

I'm trying to echo out the name of the user in my school.

For school table

    Schema::create('schools', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('user_id')->unsigned();
    $table->foreign('user_id')->references('id')->on('users');
        $table->string('school_name');
        $table->string('status');
        $table->string('gender');
        $table->string('notes');
        $table->string('member_name');
        $table->string('type');
        $table->string('file_number');
        $table->string('phone');
        $table->string('address');
    });

For SchoolController

public function show(School $school)
{
    $province_names = Province::all();
    $city_names = City::all();
    $center_names = City::all();
    return view('school.all', compact('school','city_names', 'province_names', 'center_names'));
}

For model School

public function user()
{
    return $this->belongsTo(User::class, 'user_id');
}

all.blade.php

{{ $school->user->firstـname }}

I get this error

Trying to get property of non-object

but I writted {{ $school->user->}} display null.

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.