Trying to get property 'name' of non-object

Posted 1 year 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.

Reply to

Use Markdown with GitHub-flavored code blocks.