Laravel查询以获取最大数量的前10条记录

时间:2019-12-17 09:39:38

标签: mysql laravel

我想根据最大数量获得前10条记录。我尝试了以下代码

1)  $topclaims = Claim::take(10)->max('estimated_loss')->get()   

           // giving me an error Call to a member function get() on string

2)  $topclaims = Claim::take(10)->orderBy('estimated_loss','DESC')->get();

           //this one is working but not getting an exact result

3) $topclaims = Claim::orderBy('estimated_loss','DESC')->limit(10)->get(); 

     //this one is also working but not getting an exact result

我想要这样

first record = 10,
second = 9,
third = 8,
fourth = 7,
fifth = 6


the result I am getting is 

first record = 9,
second = 8,
third = 10,
fourth = 6,
fifth = 7

1 个答案:

答案 0 :(得分:3)

您必须先将estimated_loss转换为int才能进行排序。

$topclaims = Claim::orderByRaw("CAST(estimated_loss as UNSIGNED) DESC")->limit(10)->get();