Laravel return one record where max value

Viewed 17656

I'm trying to return one record with a max value as an object. When I do this:

public function highestExpense()
    {
        return auth()->user()->expenses()->max('amount');
    }

It works, and I get the highest value. But I want to have the whole record returned, so I also have access to the expense name, created_at, and so on.

This didn't work for me:

return auth()->user()->expenses()->max('amount')->get();

It seems like a simple solution but the answer is nowhere to be found.

3 Answers

You can the Collection method sortByDesc like follow :

return auth()->user()->expenses()->sortByDesc('amount')->first();
Related