Laravel - getting the largest object based on attribute value

I am trying to get the four highest objects based on the attribute in them.

I can successfully get the one with the highest value by doing the following:

{{ Bid::where('auction_id', '=', $auction->id)->max('bid_amount') }}

What gets the object with the highest value bid_amount.

Now I could create a loop and encode all the bets, but Laravel should have a smarter way to do this, and I could not find it in my documentation.

+2
source share
1 answer

You can try this

Bid::where('auction_id', '=', $auction->id)
    ->orderBy('bid_amount', 'DESC')
    ->take(4)
    ->get();
+1
source

Source: https://habr.com/ru/post/1538114/


All Articles