Xavier Issac
Xavier Issac

Reputation: 495

Laravel Distinct query is not working in my project

In my laravel project, am joining more than 2 tables, but the problem is that in the table location_services,There is multiple entries with same location_id and services_id. I want to take only one row in such cases.Following is my Query. What is the problem here.Kindly help me to resolve it

   $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(); 

Please help.

I want to retrive all data from clinics table which have various services in various locations. Eaach location may or may not have more than one services

Upvotes: 0

Views: 78

Answers (1)

unclexo
unclexo

Reputation: 3941

You may use groupBy() method to achieve the goal. Just add this method before the get() method's call.

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

Upvotes: 1

Related Questions