Paged Index helps you build server-driven, paginated listings in Laravel applications. It exposes a fluent API for combining filtering, sorting, eager-loading relationships, pagination, and optional resource transformation into a single response object.
- Laravel 11 or 12
- Install the package:
composer require m3team/paged-index
- (Optional) Publish the configuration to customize request key names and defaults:
php artisan vendor:publish --tag=paged-index-config
By default, the library reads these query-string keys (all configurable in config/paged-index.php):
page_index– zero-based page number.page_size– number of items per page.sort_column– allowed column or key defined viaallowedSorts().sort_direction–ascordesc.filters[]– keyed filter values matched againstallowedFilters().relationships[]– relationships to eager load when using an Eloquent builder, subject toallowedRelationships().
Use PagedIndex::fromRequest() to turn an Eloquent or query builder into a paginated response.
use App\Http\Resources\UserResource;
use App\Models\User;
use M3Team\PagedIndex\PagedIndex;
class UserController
{
public function index()
{
return PagedIndex::fromRequest(User::query(), UserResource::class)
->allowedSorts(['id', 'name', 'email'])
->allowedFilters([
'name' => 'name', // simple where
'email' => fn ($q, $value) => $q->where('email', 'like', "%{$value}%"),
])
->allowedRelationships([
'posts',
'profile' => fn ($query) => $query->where('active', true),
])
->getObjects();
}
}The returned PagedIndexCollection JSON structure is:
{
"objects": [/* transformed items */],
"total": 42,
"page_index": 0,
"page_size": 15
}- Query parameters are validated and merged with defaults.
- Allowed relationships are eager loaded only when they are allowlisted with
allowedRelationships()and only on Eloquent builders. - Sorting and filtering are applied using the
allowedSorts/allowedFiltersmap you provide. Each entry can be a column name or a closure receiving the builder. - Pagination skips/limits the query when
page_sizeis greater than zero. - The collection is optionally transformed with a Laravel API resource class, then wrapped in
PagedIndexCollection.
Create a plain application class for paged-index composition:
php artisan make:paged_index UserPagedIndexClasses are placed in app/Http/PagedIndexes.
The legacy DatabasePagedIndex remains for backward compatibility but is deprecated as of v5. Use PagedIndex for new code.