Level 70
Try this one-
$truckcount = Trip::where('client_id', $userClientId)
->select(\DB::raw("COUNT(DISTINCT truck_no) as count"))
->get();
5 likes
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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?
Try this one-
$truckcount = Trip::where('client_id', $userClientId)
->select(\DB::raw("COUNT(DISTINCT truck_no) as count"))
->get();
Please or to participate in this conversation.