Installation:
composer require cannibal/pagination-bundle
Add to config/app.php under providers:
Cannibal\PaginationBundle\PaginationBundle::class,
First Use Case: Paginate a simple Eloquent collection:
use Cannibal\PaginationBundle\Pagination;
$items = Item::all();
$paginated = Pagination::make($items, 10); // 10 items per page
return view('items.index', compact('paginated'));
Where to Look First:
src/PaginationBundle/PaginationBundle.php for core logic.app/Pagination.php for quick access.resources/views/vendor/pagination for default templates.Basic Pagination:
$paginated = Pagination::make($collection, $perPage);
Arrayable/Jsonable objects.URL-Based Pagination:
$paginated = Pagination::make($collection, $perPage, request('page', 1));
?page=N query params.Custom Views: Override default templates by publishing assets:
php artisan vendor:publish --tag=pagination.views
Modify files in resources/views/vendor/pagination.
API Responses:
return response()->json($paginated->toArray());
Returns standardized JSON with data, meta (pagination info), and links.
Dynamic Per-Page:
$perPage = request('per_page', 10);
$paginated = Pagination::make($collection, $perPage);
toArray() for search results:
$searchResults = Item::search('query')->paginate(15);
return Pagination::make($searchResults->items(), 15)->toArray();
paginated object directly to components for reactive updates.Pagination facade for unit tests:
$this->partialMock(Pagination::class, function ($mock) {
$mock->shouldReceive('make')->andReturn($mock->makePartial());
});
Query String Conflicts:
route() in links, ensure page param doesn’t clash with existing query strings.?page={$paginated->currentPage} explicitly.Arrayable Objects:
Arrayable or Jsonable.collect() or use toArray() manually.Caching:
$cachedItems = Cache::remember("items_{$perPage}", now()->addHours(1), function() use ($perPage) {
return Item::paginate($perPage);
});
Bootstrap CSS Dependency:
bootstrap: false in config:
'templates' => [
'bootstrap' => false,
],
\Log::debug('Pagination meta:', $paginated->meta());
php artisan config:clear
If pagination links break after updates.Custom Meta Data:
Add extra metadata to the meta array:
$paginated->withMeta(['custom_key' => 'value']);
Link Generators: Override link generation for non-route-based URLs:
Pagination::macro('generateUrl', function ($page) {
return url("custom/path?page=$page");
});
Event Listeners:
Listen for pagination events (e.g., PaginationGenerated):
Pagination::addListener(function ($paginated) {
// Modify $paginated before rendering
});
Localization: Override labels (e.g., "Previous", "Next") via config:
'labels' => [
'previous' => 'Prev',
'next' => 'Next',
],
How can I help you explore Laravel packages today?