pagerfanta/doctrine-collections-adapter
Adapter connecting Pagerfanta pagination to Doctrine Collections. Paginate ArrayCollection and other Collection implementations with limit/offset slicing and count support, enabling easy integration of Doctrine collection data into Pagerfanta-based UIs.
pagerfanta/doctrine-collections-adapter enables server-side pagination for Doctrine ORM/ODM collections, addressing memory and performance bottlenecks in Laravel applications where Doctrine is used alongside or instead of Eloquent. It integrates seamlessly with Pagerfanta, a mature pagination library, to provide chunked, iterable access to large collections without loading them entirely into memory. This is particularly valuable for admin dashboards, reporting tools, or hybrid Laravel/Doctrine systems where Eloquent’s built-in pagination is insufficient or incompatible.EntityRepository::findAll() or custom queries). It is not a replacement for Laravel’s Eloquent pagination or SQL-level pagination (e.g., LIMIT/OFFSET). The adapter operates at the collection abstraction layer, making it ideal for scenarios where collections are already materialized (e.g., after complex joins or dynamic filtering).LengthAwarePaginator, this adapter does not provide built-in HTTP request parsing or view integration, necessitating additional boilerplate.doctrine/collections.PaginationServiceProvider (e.g., method name collisions like currentPage()).paginate() or simplePaginate() are preferred.LIMIT/OFFSET.pagerfanta/pagerfanta and doctrine/collections to avoid breaking changes.Illuminate\Pagination). Increases cognitive load for developers unfamiliar with Pagerfanta’s API.paginate() or cursor() for SQL-level pagination.setFirstResult()/setMaxResults() for SQL pagination.LIMIT/OFFSET) infeasible due to query complexity?UserRepository::findAll()) for table views or exports./admin/users or /reports/orders.Illuminate\Pagination\LengthAwarePaginator).Dependency Installation:
composer require pagerfanta/pagerfanta pagerfanta/doctrine-collections-adapter
composer.json to avoid breaking changes:
"require": {
"pagerfanta/pagerfanta": "^4.0",
"pagerfanta/doctrine-collections-adapter": "^1.0",
"doctrine/collections": "^2.0"
}
Adapter Setup:
use Pagerfanta\Pagerfanta;
use Pagerfanta\Adapter\DoctrineCollectionAdapter;
$collection = $entityManager->getRepository(User::class)->findAll();
$adapter = new DoctrineCollectionAdapter($collection);
$pagerfanta = new Pagerfanta($adapter);
$pagerfanta->setMaxPerPage(20); // Configure page size
$currentPage = $request->input('page', 1);
$pagerfanta->setCurrentPage($currentPage);
Laravel Integration Strategies:
class UserPaginatorService {
public function paginateCollection(Collection $collection, int $perPage, int $page): Pagerfanta {
$adapter = new DoctrineCollectionAdapter($collection);
$pagerfanta = new Pagerfanta($adapter);
$pagerfanta->setMaxPerPage($perPage);
$pagerfanta->setCurrentPage($page);
return $pagerfanta;
}
}
@foreach ($pagerfanta as $user)
<tr><td>{{ $user->name }}</td></tr>
@endforeach
{{ $pagerfanta->getLinks('bootstrap_4') }}
JsonResponse:
return response()->json([
'data' => $pagerfanta->getCurrentPageResults(),
'meta' => [
'total' => $pagerfanta->getNbResults(),
'page' => $pagerfanta->getCurrentPage(),
'per_page' => $pagerfanta->getMaxPerPage(),
],
]);
Dependency Injection (Optional):
$app->bind(Pagerfanta::class, function ($app) {
return new Pagerfanta(new DoctrineCollectionAdapter($app->make(Collection::class)));
});
2.1.5). Avoid edge cases with Criteria-filtered collections.currentPage() method if Laravel’s PaginationServiceProvider is active.How can I help you explore Laravel packages today?