Trying to get property of non-object

Posted 1 year 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) {

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');


{{ $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.