Php Laravel 5.3按集合排序的分页
为什么paginate方法在这个例子中不起作用Php Laravel 5.3按集合排序的分页,php,mysql,laravel,collections,eloquent,Php,Mysql,Laravel,Collections,Eloquent,为什么paginate方法在这个例子中不起作用 $shopIds = Follower::whereUserId($user->id)->orderBy('created_at','desc')->get()->pluck('shop_id')->toArray(); $shops = Shop::whereIn('id',$shopIds)->with('deals')->with('deals.likes')->pagi
$shopIds = Follower::whereUserId($user->id)->orderBy('created_at','desc')->get()->pluck('shop_id')->toArray();
$shops = Shop::whereIn('id',$shopIds)->with('deals')->with('deals.likes')->paginate($this->perPage)->sortBy(function($likes) {
return $likes->count();
});
dd($shops);
谢谢你的帮助;) paginate工作正常,但是
sortBy
方法给您带来了问题,因为当您使用sortBy
时,它会返回一个新集合
最后,您的$shops
是illumb\Support\Collection
的一个实例,而不是illumb\Pagination\lengtashareapaginator
您可以通过以下方式进行尝试:
$paginated_shops = Shop::whereIn('id',$shopIds)
->with('deals')
->with('deals.likes')
->paginate($this->perPage);
$shops = $paginated_shops->sortBy(function($likes) {
return $likes->count();
});
$shops = new LengthAwarePaginator($shops, $paginated_shops->total(), $paginated_shops->perPage());
记住在类的顶部添加use
语句,如下所示:
use Illuminate\Pagination\LengthAwarePaginator;
就像@Amit提到的,$shops是一个集合,因此不是一个复制者。您将不得不构建分页器并自己进行切片。。。不太复杂 例如,假设您正在按路线行驶,呼叫控制器。。。然后将结果返回到视图。。。现在,将使用请求中的分页信息请求路由,您的任务是将结果$shops放入一个数组中,将数组切片为页面的正确结果,并将其返回到视图中。。。我没有运行此代码。。。以这个为例。。。但它看起来像:
public function yourController{
// This will return a collection... we will convert it to array later
$shops = Shop::whereIn('id',$shopIds)->with('deals')->with('deals.likes')->paginate($this->perPage)->sortBy(function($likes) {
return $likes->count();
});
$shopsArray = $shops->toArray();
// Here we deal with the slicing... you need to slice the page you are
// at the paging info is in the request...
// this basically gets the request's page variable... or defaults to 1
$page = Paginator::resolveCurrentPage('page') ?: 1;
// Assume 15 items per page... so start index to slice our array
$startIndex = ($page - 1) * 15;
// Length aware paginator needs a total count of items... to paginate properly
$total = count($shopsArray);
// Eliminate the non relevant items by slicing the array to page content...
$results = array_slice($shopsArray, $startIndex, 15);
$result = new LengthAwarePaginator($results, $total, 15, $page, [
'path' => Paginator::resolveCurrentPath(),
'pageName' => 'page',
]);
return view('yourView', compact('result'));
}
谢谢你们
我就这样修好的
$shopIds = Follower::whereUserId($user->id)->orderBy('created_at','desc')->get()->pluck('shop_id')->toArray();
$shopIds = Shop::whereIn('id',$shopIds)->with('deals')->with('deals.likes')->get()->sortBy(function($likes) {
return $likes->count();
})->pluck('id')->toArray();
$orderedIds = implode(',',$shopIds);
$shops = Shop::whereIn('id', $shopIds)->whereNotIn('user_id', [$user->id])->orderByRaw(\DB::raw("FIELD(id, ".$orderedIds." )"))->paginate($this->perPage);
dd($shops);
现在我有了一个Illumb\Pagination\LengthAwarePaginator的实例
谢谢这里有什么错误?没有错误,但是如果我使用paginate()方法或get()方法,结果是一样的。结果没有分页,所以我不明白为什么。您不需要进行新的查询。您拥有分页的所有数据。这是一个多余的查询。查看我的最新答案,它可以解决您的问题,而无需进行额外的查询。这在2021年帮助了我:)非常感谢。