Wednesday, April 12, 2017

Fetching data from group by having min value in laravel

I want to fetch the data from database in laravel, which looks like this.

id | model_id | price
1 |1 | 200
2 |1 | 100
3 |2 | 500
4 |2 | 300
5 |2 | 400

I want the result as :

id | model_id | price
2 |1 | 200
4 |2 | 300

This is my code :

Model::modelfilter(Input::only('model_id', 'price'))->groupBy('model_id')->havingRaw('MIN(price)')->get();

Thank you.



via Sunil T C

Advertisement