0

I am using Laravel-5.8 Eloquent to run a query. I want to get the total trucks utilized by a particular client for all their trips without counting a truck twice. I used the query below, but there is repetition of trucks used more than once:

  $truckcount = Trip::where('client_id', $userClientId)
   ->select(\DB::raw("COUNT(truck_no) as count"))
   ->distinct()   
   ->get();

How do I write the correct Eloquent query to achieve this?

ayobamilaye
  • 429
  • 2
  • 10
  • 25

1 Answers1

1

try this:

 $truckcount = Trip::where('client_id', $userClientId)
   ->select(\DB::raw("COUNT(DISTINCT truck_no) as count"))
   ->get();