Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

szalikdev's avatar

Eloquent question: Is there a way to perform count query for column with the same id?

I'm trying to find a way to execute this query: SELECT carId, COUNT(1) FROM event_dataGROUP BYcarId ORDER BY COUNT(1) DESC but with Models, i didn't find anything in docs so i came here

0 likes
4 replies
Sinnbeck's avatar

@szalikdev What do you mean? Using a model?

EventData::query()
->selectRaw('carId, COUNT(1)')
->groupBy('carId')
->orderByRaw('COUNT(1)', 'desc')
->get()
Sinnbeck's avatar
DB::table('event_data')
->selectRaw('carId, COUNT(1)')
->groupBy('carId')
->orderByRaw('COUNT(1)', 'desc')
->get()
1 like

Please or to participate in this conversation.