Level 122
use relationships rather than joins
you will then be able to do
$hotel = Hotel::with('rooms', 'city')->find(1);
1 like
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hello,
I made a query which looks like this:
$hotel = App\Hotel::where('hotel_id', 2)
->join('rooms', 'rooms.hotel_id', '=', 'hotels.id')
->join('cities', 'hotels.city_id', '=', 'cities.id')
->select('hotels.name as hotel_name', 'cities.name as city_name', 'rooms.name as room_name')
->get();
Which outputs:
Illuminate\Support\Collection {#852
all: [
{#847
+"hotel_name": "testHotel",
+"city_name": "Guba",
+"room_name": "testroom1",
},
{#848
+"hotel_name": "testHotel",
+"city_name": "Guba",
+"room_name": "testroom2",
},
],
}
As you can see hotel_name and city_name appear for every room. It's fine, but is it possible to make it something like this:
all: [
+"hotel_name": "testHotel",
+"city_name": "Guba",
{#847
+"room_name": "testroom1",
},
{#848
+"room_name": "testroom2",
},
],
}
I want to you use this in the Form Model binding.
Thanks.
use relationships rather than joins
you will then be able to do
$hotel = Hotel::with('rooms', 'city')->find(1);
Please or to participate in this conversation.