Why would the bookings and user share the id ?
Anyways. You just want the first booking ?
$booking = App\Models\User::find($id)->bookings()->first();
$booking->rooms;
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi there, I'm currently working on a booking application and am trying to figure out my database schemes and Eloquent relationships. I have the following tables: users, bookings, rooms, and booking_rooms. A booking belongs to many rooms, and a room belongs to many bookings. I managed to implement the many to many relationship successfully, and can retrieve rooms associated with a particular booking by using
$booking = App\Models\Booking::find($id);
$booking->rooms;
However, each booking belongs to a single user. If I do
$booking = App\Models\User::find($id)->bookings->where('id', $id);
$booking->rooms;
I am met with the following error:
Exception with message 'Property [rooms] does not exist on this collection instance.'
Why is that?
Let's break this statement in pieces:
$booking = App\Models\User::find($id)->bookings->where('id', $id);
App\Models\User::find($id)
User::find() will execute a query in the users table and return a User instance.
So up here you will have a User object
App\Models\User::find($id)->bookings
You are now asking Laravel to execute a query and retrieve all bookings belonging to the user.
Note you define this relationship as a method, but when we call it as a property, Laravel understands you want it to execute a query and return its results.
As the relation is a belongs to many, we end up with a Collection instance here.
App\Models\User::find($id)->bookings->where('id', $id)
Now you are calling the Collection@where method. This method filters down the collection, but the result is still a collection object, even if it has only an item.
https://laravel.com/docs/9.x/collections#method-where
Side note: I guess here you would want to use a different
$idthan the one used to find the user. Most probably you want a specific booking id.
So when calling
$booking->rooms;
Laravel errors out saying there is no rooms property on the collection object filtered in the last statement we broke down.
You can use a different collection method to filter down to a single instance, both the following will do, but you can look at the docs on more:
$booking = App\Models\User::find($id)->bookings->where('id', $id)->first();
or
$booking = App\Models\User::find($id)->bookings->firstWhere('id', $id);
Hope this helps =)
Please or to participate in this conversation.