Trying to get property 'name' of non-object

Posted 10 months ago by pickab00

I am using with(). Everything checks out. I am getting the related data as well when i manually try to display it using controller.

Here is my db structure:

room_cats (id, name)

rooms (id, room_cats_id, roomnumber)

Here is my Room Model:

public function roomcat(){
        return $this->belongsTo('App\RoomCat', 'room_cats_id', 'id');

Here is my code controller:

public function dashboard()
        $room = Room::with('roomcat')->get();

        foreach($room as $rooms){
          echo  $rooms->room_cats_id->name;

I don't know whats causing the error. When i use "return $room" it gives me the results collection with the relation detail. But the above controller code does not work.

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

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up


Reply to

Use Markdown with GitHub-flavored code blocks.