sammyt/paginate-collection
最新稳定版本:v1.0.1
Composer 安装命令:
composer require sammyt/paginate-collection
包简介
Allows laravel users to paginate a collection via a global helper method. Currently laravel only allows for native query builder pagination.
README 文档
README
Description:
Allows laravel users to paginate a collection via a global helper method. Currently laravel only allows for native query builder pagination.
Usage:
As a simple inline example I created a fresh Laravel project using:
laravel new MyProject
In MyProject:
- Set up an
.envfile which connects to a local database. - Run the out-of-the-box migrations making sure the
userstable is created. - Use
tinkerto create some test users in theuserstable or create them manually in your DB
To test you can call this method globally and pass in your collection. Other method parameters are optional. Accepted parameters include:
$collection=> Required$perPage=> Default is 15 but also accepts anint$currentPage=> Default is null but also accepts anint$options=> Any additional options you wish to pass
Below is an example use case of how to use the paginate_collection global helper method.
Follow steps 1-3 above and then create a route in web.php which has a callback function which will allow you to return a collection for testing
Route::get('/users', function() {
$userCollection = User::all();
$paginatedUsersCollection = paginate_collection(
$userCollection,
10,
null
);
dd($paginatedUsersCollection);
});
Output
Illuminate\Pagination\LengthAwarePaginator {#251 ▼
#total: 2
#lastPage: 1
#items: Illuminate\Support\Collection {#265 ▶}
#perPage: 10
#currentPage: 1
#path: "/"
#query: []
#fragment: null
#pageName: "page"
+onEachSide: 3
#options: []
}
As you can see it uses the LengthAwarePaginator behind the scenes and your collection is now paginated.
统计信息
- 总下载量: 1.01k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-10-01