通过总和关系订购时的Paginate

时间:2017-08-27 13:15:35

标签: php laravel eloquent

此请求中有什么问题?

$users = User::where('about', '!=', null)->with('tips')->paginate(12)->sortBy(function($user){
    return $user->tips->sum('amount');
})->reverse();

在视图中有分页代码

{{$users->links()}}

我有错误

Method links does not exist. (View: /Users/fab/Sites/tipme/resources/views/users.blade.php)

1 个答案:

答案 0 :(得分:0)

我找到了解决方案

use Illuminate\Pagination\LengthAwarePaginator;

$paginated_users = User::where('about', '!=', null)->with('tips')->paginate(12);

$users = $paginated_users->sortBy(function($user) {
    return $user->tips->sum('amount');
})->reverse();

$users = new LengthAwarePaginator($users, $paginated_users->total(), $paginated_users->perPage());

有效!