我要通过以下方式从严格表中检索数据:
$data = Accommodation::join("hotels", "accommodations.hotel_id", "=", "hotels.id")
->join("room_types", "accommodations.room_type", "=", "room_types.id")
->join("country", "accommodations.country_id", "=", "country.id")
->join("accommodation_accreditation", "accommodations.id", "=", "accommodation_accreditation.accommodation_id")
->join("accreditation", 'accreditation.id', "=", "accommodation_accreditation.accreditation_id")
->select('hotels.name as Hotel' ,'room_types.type as Room', 'price_per_night as Price per Night', 'check_in as Check In', 'check_out as Check Out', 'nights as Nights', 'sub_total as Sub Total', 'country.name as Country', 'accreditation.full_name as Occupants')
->get()->toArray();
我会收到一个房间里每个人的一行。
Room Price per Night Check In Check Out Nights Sub Total Country Occupants
---------------------- ----------------- ------------ ------------ -------- ----------- --------- -------------------
Twin/Double standart 160 2019-04-10 2019-04-15 5 800 UKR Vlad Timochenko
Twin/Double standart 160 2019-04-10 2019-04-15 5 800 UKR Selena Viachenko
我需要隐藏住户的名字,并像这样将它们排成一排:
Room Price per Night Check In Check Out Nights Sub Total Country Occupants
---------------------- ----------------- ------------ ------------ -------- ----------- --------- ------------------------------------
Twin/Double standart 160 2019-04-10 2019-04-15 5 800 UKR Vlada Nikolchenko, Olena Diachenko
这是我正在讨论的表的模式
希望很清楚。如果您能帮上忙,那就太好了。
答案 0 :(得分:0)
说实话,我从不同的地方找到了完整的解决方案。
当萨姆感到很轻松时,我使用了GROUP_CONCAT
。我的代码如下:
$data = Accommodation::join("hotels", "accommodations.hotel_id", "=", "hotels.id")
->join("room_types", "accommodations.room_type", "=", "room_types.id")
->join("country", "accommodations.country_id", "=", "country.id")
->join("accommodation_accreditation", "accommodations.id", "=", "accommodation_accreditation.accommodation_id")
->join("accreditation", 'accreditation.id', "=", "accommodation_accreditation.accreditation_id")
->select('hotels.name as Hotel' ,'room_types.type as Room', 'price_per_night as Price per Night', 'check_in as Check In', 'check_out as Check Out', 'nights as Nights', 'sub_total as Sub Total', 'country.name as Country', DB:raw("GROUP_CONCAT('accreditation.full_name SEPRATOR ',') as `Occupants`"))
->groupBy('accommodation.id')
->get()->toArray();
我要警惕的另一件事是在Occupants标签上使用反引号,否则SEPARATOR无法正常工作。
还有其他类似的线程,所以这是重复的。
Group_concat - laravel eloquent how to use GROUP_CONCAT in laravel