简体   繁体   English

Laravel 5.3 对 sortBy 集合进行分页

[英]Laravel 5.3 pagination on sortBy collection

Why the paginate method doesn't work on this example ?为什么 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')->paginate($this->perPage)->sortBy(function($likes) {
                return $likes->count();
            });
            dd($shops);

在此处输入图片说明

Thank's for help ;)感谢帮助 ;)

The paginate is just working fine but the sortBy method is creating the problem for you because when you use sortBy it returns a new collection. paginate工作正常,但sortBy方法为您带来了问题,因为当您使用sortBy它会返回一个新集合。

So finally your $shops is an instance of Illuminate\\Support\\Collection not of Illuminate\\Pagination\\LengthAwarePaginator .所以最后你的$shopsIlluminate\\Support\\Collection的实例,而不是Illuminate\\Pagination\\LengthAwarePaginator

You can try it as:您可以尝试如下:

$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());

Remember to add use statement at the top of the class as:记得在类的顶部添加use语句:

use Illuminate\Pagination\LengthAwarePaginator;

Thank you guys!谢谢你们!

I fix it like that我是这样修的

$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);

and now I have an instance of Illuminate\\Pagination\\LengthAwarePaginator现在我有一个 Illuminate\\Pagination\\LengthAwarePaginator 的实例在此处输入图片说明

Thank's谢谢

Like @Amit mentionned, $shops is a collection, thus not a LengthAwarePaginator.就像@Amit 提到的那样,$shops 是一个集合,因此不是 LengthAwarePaginator。 You are going to have to build the paginator and slice it yourself... not too complicated...你将不得不构建分页器并自己切片......不要太复杂......

For example, let's assume you are hitting your route, calling your controller... then returning results to a view... Now that route is going to get requested with the pagination info in the request, you job is to get the result $shops into an array, slice the array into the right result for the page and return that to your view... I have not run this code... so take this as an example... but it will look something like:例如,假设您正在访问您的路线,调用您的控制器...然后将结果返回到视图...现在将使用请求中的分页信息请求该路线,您的工作是获取结果 $存储到一个数组中,将数组切片为页面的正确结果并将其返回到您的视图中...我还没有运行此代码...所以以此为例...但它看起来像:

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'));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM