A
A
Anatoly2018-07-02 13:24:33
ORM
Anatoly, 2018-07-02 13:24:33

Laravel ORM groupBy?

Good afternoon!
Plzz tell me how to make a request?
I need to select one rubric_id record with the maximum value in another field.
5b39fd5454529538455770.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey Novikov, 2018-07-02
@les-anatoliy

There should be something like this if models and relays are configured

Model::with('relation_to_other_model', function($query) {
    $query->orderBy('some_field', 'desc')->take(1);
})

A
anlamas, 2018-07-02
@anlamas

Model::select(DB::raw('MAX(`field`)'))->groupBy('rubric_id')->get()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question