sammyt / paginate-collection
Allows laravel users to paginate a collection via a global helper method. Currently laravel only allows for native query builder pagination.
Installs: 1 009
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/sammyt/paginate-collection
This package is auto-updated.
Last update: 2025-12-29 03:27:03 UTC
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.