Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mozew's avatar
Level 6

Trying to get property of non-object

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.

0 likes
30 replies
mozew's avatar
Level 6

I've tried it before. i get this error.

Trying to get property of non-object

Sergiu17's avatar

@irankhosravi so you pass $school variable to the view

public function show(School $school)
{
    return view('school.all', compact('school'));
}

but user ins't eager loaded, you have to be explicit, you have two options

1 First

In your school model

class School extends Model
{
    protected $with = ['user'];
}

Everywhere you access $school, user will be eager loaded.

2 Second option

public function show(School $school)
{
    $school->load('user');
    return view('school.all', compact('school'));
}
mozew's avatar
Level 6

@Sergiu17 I get this error

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users.schools_list_id' in 'where clause' (SQL: select * from users where users.school_id in ())

Sergiu17's avatar
class School extends Model
{
    protected $with = ['users'];

    public function users()
    {
         return $this->hasMany(User::class);
    }
}
public function show(School $school)
{
    return view('school.all', compact('school'));
}

Does this work?

1 like
Snapey's avatar

what @sergui suggested will not work unless users table has a school_id column, but shools table has user_id column so belongsTo is the correct relationship

BishoyWagih's avatar

make sure all schools table records has user_id value, it may be one record doesn't have a user_id,

i see your code is correct..

or you can delete all records from schools table and create one record, then test your code..

mozew's avatar
Level 6

@BishoyWagih

I tried and checked in 12 hours all file model controller database blade but I get this error.

Trying to get property of non-object

I go to model change to hasMany and hasOne and blengsTo and........ but this is not display

ChrisSFR's avatar

Can you add a dd in your public function show method and check the output. Is the user loaded there?

dd($school->toArray())

and

dd($school->user->toArray())
mozew's avatar
Level 6

I checked dd($school->toArray()) it display []

And dd($school->user->toArray()) it display error

Call to a member function toArray() on null

BishoyWagih's avatar

if the school collection is empty so user will not be displayed

if you are using laravel >= 5.5 you can do this.

optional($school->user)->firstـname

it will not show an error..

just try and update with results..

mozew's avatar
Level 6

No I am using Laravel 5.4. Furthermore all data even school collection in database are fulls.

BishoyWagih's avatar

try

{{ $school->user ?  $school->user->firstـname ? '' }}
1 like
ChrisSFR's avatar

If the School Variable returns an empty array than Maybe something is wrong with your route model binding. Can you post your Route method in your Web.php

mozew's avatar
Level 6

Did not show I want toshow

{{ $schoolsList->user ?  $schoolsList->user->firstـname  : '' }}
Melodia's avatar

Normally when you get "Trying to get property of non-object" it means that whatever record you are trying to get from the database does not exist.

Cronix's avatar
  1. Show the route for this
  2. Show an example link that you are using to access the route. Not your code, an actual example link like http://yoursite.dev/schools/4

If

public function show(School $school)
{
    dd($school);
}

outputs nothing or null, then your school isn't being retrieved. Nothing else but that matters right now.

mozew's avatar
Level 6

I did not show any school in the browser,, but I tried to change the SHOW method.

public function show(School $school)
{
    $school = School::findOrFail(id);
    return $school;
}

It displayed. then I change.

public function show(School $school)
{
    $schools = School::findOrFail($school);
    return view('school.all', compact'schools');
}

I get this this error.

Use of undefined constant id - assumed 'id'

Cronix's avatar

Ok, well you're not showing your route or an example link, so I hope you figure it out.

mozew's avatar
Level 6
$this->resource('schools', 'SchoolController');
Cronix's avatar

I'm not sure how you think we can help if you don't provide the info we ask for in order to help you. I asked for 2 things. You didn't respond and posted about other things. I asked again. You only posted 1 of the 2 things. So, still not enough info to help you, sorry! I tried...

mozew's avatar
Level 6

@Cronix

Excuse me I made it myself.

public function show($id)
{
    $school = School::with('user')->where('id', $id)->first();
    //..
}

You and me are friends.

Good luck.

softopia's avatar

@irankhosravi I think, using this code you can only fetch first result if you want to show all results in view you should use get() and use foreach loop in view.

  $school = School::with('user')->where('id', $id)->first();

Please or to participate in this conversation.