Laravel - getting largest object based on attribute value

纵然是瞬间 提交于 2019-12-12 18:35:22

问题


I have been trying to get the four highest objects based on an 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') }}

Which gets the object which have the highest bid_amount value.

Now I could potentially make a loop and loop all bids through but Laravel must have a smarter way of doing it and I have not been able to find this in their documentation.


回答1:


You may try this

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


来源:https://stackoverflow.com/questions/19835362/laravel-getting-largest-object-based-on-attribute-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!