这是我尝试分页的方式:
$posts = Post::all()->sortByDesc("created_at")->pagination(1);
但我收到此错误:
方法 Illuminate\Database\Eloquent\Collection::pagination 不存在。
创建助手类
<?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 文件。之后要添加一个帮助文件。 Composer 有一个文件键(它是文件路径数组),您可以在自动加载内部定义它:
"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;
});
您可以在方法 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,
]
);
});
这是 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,
]
);
});
您可以通过删除所有内容轻松分页:
$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);
}
试试这个代码:
//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;
});