下面是我如何尝试分页:
$posts = Post::all()->sortByDesc("created_at")->pagination(1);
但我得到这个错误:方法Illuminate\Database\Eloquent\Collection::分页不存在。
8zzbczxx1#
创建helper类
<?php namespace App\Helpers; use Illuminate\Container\Container; use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Pagination\Paginator; use Illuminate\Support\Collection; class PaginationHelper { public static function paginate(Collection $results, $showPerPage) { $pageNumber = Paginator::resolveCurrentPage('page'); $totalPageNumber = $results->count(); return self::paginator($results->forPage($pageNumber, $showPerPage), $totalPageNumber, $showPerPage, $pageNumber, [ 'path' => Paginator::resolveCurrentPath(), 'pageName' => 'page', ]); } /** * Create a new length-aware paginator instance. * * @param \Illuminate\Support\Collection $items * @param int $total * @param int $perPage * @param int $currentPage * @param array $options * @return \Illuminate\Pagination\LengthAwarePaginator */ protected static function paginator($items, $total, $perPage, $currentPage, $options) { return Container::getInstance()->makeWith(LengthAwarePaginator::class, compact( 'items', 'total', 'perPage', 'currentPage', 'options' )); } }
打开composer.json文件后,要添加一个helpers文件,composer有一个files键(这是一个数组的文件路径),您可以定义内的autoload
"autoload": { "files": [ "app/Helpers/PaginationHelper.php" ], "classmap": [ "database/seeds", "database/factories" ], "psr-4": { "App\\": "app/" } },
现在你必须在终端中输入这个命令
composer dump-autoload
现在你可以像下面的例子一样创建一个集合的分页
Route::get('/test_collect_pagintae', function () { $users = \App\User::get(); $showPerPage = 20; $paginated = PaginationHelper::paginate($users, $showPerPage); return $paginated; });
3bygqnnd2#
这是因为paginate是Builder方法,而不是集合。您需要手动创建分页器,如何在这里描述-https://laravel.com/docs/8.x/pagination#manually-creating-a-paginator
paginate
Builder
wooyq4lh3#
你可以在app/provider/serviceProvider中的方法 Boot 中使用这个代码
app/provider/serviceProvider
Collection::macro('paginate', function($perPage, $page = null, $pageName = 'page') { $page = $page ?: LengthAwarePaginator::resolveCurrentPage($pageName); return new LengthAwarePaginator( $this->forPage($page, $perPage), // $items $this->count(), // $total $perPage, $page, [ // $options 'path' => LengthAwarePaginator::resolveCurrentPath(), 'pageName' => $pageName, ] ); });
tktrz96b4#
SOLUTION这是simonhamp提供的非常好的解决方案,感谢simonhamp我的建议是像下面这样在AppServiceProvider中执行-〉values()。这很重要,因为当切片收集时,键被保留,我们不希望分页器这样。
Collection::macro('paginate', function ($perPage, $total = null, $page = null, $pageName = 'page') { $page = $page ?: LengthAwarePaginator::resolveCurrentPage($pageName); return new LengthAwarePaginator( $total ? $this : $this->forPage($page, $perPage)->values(), $total ?: $this->count(), $perPage, $page, [ 'path' => LengthAwarePaginator::resolveCurrentPath(), 'pageName' => $pageName, ] ); });
h7appiyu5#
您可以通过删除所有内容轻松分页:-
$posts = Post::sortByDesc("created_at")->pagination(1);
在我的例子中,我必须合并多个集合,然后通过合并的数据分页,所以我必须这样做:
$items = $items2->merge($items1); $items = $this->paginate($items); public function paginate($items, $perPage = 15, $page = null, $options = []) { $page = $page ?: (Paginator::resolveCurrentPage() ?: 1); $items = $items instanceof Collection ? $items : Collection::make($items); return new LengthAwarePaginator($items->forPage($page, $perPage), $items->count(), $perPage, $page, $options); }
参考:- Laravel Documentation,How to paginate Laravel collection?
hzbexzde6#
请尝试以下代码:
//convert to array $posts = Post::all()->sortByDesc("created_at")->toArray(); //Create new pagination $currentPage = LengthAwarePaginator::resolveCurrentPage(); $perPage = 3; $currentItems = array_slice($posts, $perPage * ($currentPage - 1), $perPage); //with path of current page $posts = (new LengthAwarePaginator($currentItems, count($posts ), $perPage, $currentPage))->setPath(route('posts....')); //Convert array of array to array of object $posts->each(function ($item, $itemKey) use($posts) { $posts[$itemKey] = (Object)$item; });
6条答案
按热度按时间8zzbczxx1#
创建helper类
打开composer.json文件后,要添加一个helpers文件,composer有一个files键(这是一个数组的文件路径),您可以定义内的autoload
现在你必须在终端中输入这个命令
现在你可以像下面的例子一样创建一个集合的分页
3bygqnnd2#
这是因为
paginate
是Builder
方法,而不是集合。您需要手动创建分页器,如何在这里描述-https://laravel.com/docs/8.x/pagination#manually-creating-a-paginator
wooyq4lh3#
你可以在
app/provider/serviceProvider
中的方法 Boot 中使用这个代码tktrz96b4#
SOLUTION
这是simonhamp提供的非常好的解决方案,感谢simonhamp
我的建议是像下面这样在AppServiceProvider中执行-〉values()。这很重要,因为当切片收集时,键被保留,我们不希望分页器这样。
h7appiyu5#
您可以通过删除所有内容轻松分页:-
在我的例子中,我必须合并多个集合,然后通过合并的数据分页,所以我必须这样做:
参考:- Laravel Documentation,How to paginate Laravel collection?
hzbexzde6#
请尝试以下代码: