首页 > 解决方案 > Laravel Distinct 查询在我的项目中不起作用

问题描述

在我的 laravel 项目中,我加入了 2 个以上的表,但问题是在表中location_services,有多个具有相同 location_id 和 services_id 的条目。在这种情况下,我只想占用一行。以下是我的查询。这里有什么问题。请帮我解决它

   $loc_services = Clinic::select('*')
                        ->join('locations', 'locations.clinicID', '=', 'clinics.clinicID')
                        ->join('location_services', 'location_services.locationID', '=', 'locations.locationID')
                        ->join('services', 'services.serviceID', '=', 'location_services.serviceID')
                        ->whereIn('services.serviceID',$services_id)
                        ->where('clinics.api_key','=',$apiKey) 
                        ->get(); 

请帮忙。

我想从诊所表中检索在不同位置有各种服务的所有数据。每个位置可能有也可能没有超过一项服务

标签: phplaravel

解决方案


您可以使用groupBy()方法来实现目标。get()只需在方法调用之前添加此方法即可。

groupBy('location_services.locationID', 'location_services.serviceID')

推荐阅读