James  Tomatov James Tomatov - 4 years ago 144
jQuery Question

Select max values from query - LARAVEL

I have a table with fields:
|| id || name || c_number ||
I have a query:

$query ->select('c_number', DB::raw('count(*) as total'))
->groupBy('c_number')
->get();


output:

[{"c_number":1,"total":4},{"c_number":2,"total":2},{"c_number":3,"total":2},{"c_number":4,"total":2}]


I need select from output only value(-s) of
c_number
where total is max.
Can you tell me, please, how to do that with query?

Answer Source

You could using a mixture or orderBy() and first().

->select('c_number', DB::raw('count(*) as total'))
->groupBy('c_number')
->orderBy('total', 'desc')
->first();

Hope this helps!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download