[英]How to use sortby with paginate in Laravel
Recently, I tried to order data by using appends attribute. 最近,我尝试通过使用appends属性对数据进行排序。 From this code and it works. 从这段代码,它的工作原理。
$data = $data->sortBy(function($each){
return $each->total;
});
Then I want to use it with my old pagination code as below. 然后,我想将其与下面的旧分页代码一起使用。
$data = Book::where("type", $type_id)->orderBy("id","desc")->paginate(10);
The way I want is order data by total
attribute and then paginate it. 我想要的方式是通过total
属性对数据进行排序,然后对其进行分页。
Does anyone know how to solve this? 有谁知道如何解决这个问题?
试试这个[我没有测试]:
$data = Book::where("type", $type_id)->orderBy("total")->paginate(10);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.