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.
If I'm understanding your question correctly, to get the record that has the highest amount you could just add an orderBy
and the grab the first row:
auth()->user()->expenses()->orderBy('amount', 'desc')->first();
This should give the expense
with the highest amount.
Hope this helps!
You can the Collection method sortByDesc
like follow :
return auth()->user()->expenses()->sortByDesc('amount')->first();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With