Installation
composer require pagerfanta/core
First Use Case: Basic Pagination
use Pagerfanta\Pagerfanta;
// Create a pagerfanta instance with an adapter (e.g., Doctrine, Array, etc.)
$adapter = new \Pagerfanta\Adapter\ArrayAdapter($yourDataArray);
$pagerfanta = new Pagerfanta($adapter);
// Configure pagination settings
$pagerfanta->setMaxPerPage(10); // Items per page
$pagerfanta->setCurrentPage(1); // Current page
// Get the current page data
$currentPageData = $pagerfanta->getCurrentPageResults();
Where to Look First
\Pagerfanta\Adapter namespace for data source integrations (e.g., ArrayAdapter, DoctrineORMAdapter).getCurrentPageResults(), setMaxPerPage(), and getNbPages().Adapter Integration
use Pagerfanta\Adapter\DoctrineORMAdapter;
use Doctrine\ORM\EntityManagerInterface;
$em = app(EntityManagerInterface::class);
$query = $em->createQuery('SELECT u FROM User u');
$adapter = new DoctrineORMAdapter($query);
$pagerfanta = new Pagerfanta($adapter);
Dynamic Pagination in Controllers
$currentPage = $request->get('page', 1);
$maxPerPage = $request->get('per_page', 10);
$pagerfanta->setMaxPerPage($maxPerPage);
$pagerfanta->setCurrentPage($currentPage);
View Integration
pagerfanta/twig for Twig or custom Blade logic):
return view('posts.index', [
'pagerfanta' => $pagerfanta,
'posts' => $pagerfanta->getCurrentPageResults()
]);
Lazy Loading with Iterators
Pagerfanta\Pagerfanta with iterators for large datasets:
$iterator = new \Pagerfanta\Iterator\CurrentPageIterator($pagerfanta);
foreach ($iterator as $item) {
// Process items one page at a time
}
Service Provider Binding Bind the adapter and pagerfanta in a service provider for reuse:
$this->app->bind(Pagerfanta::class, function ($app) {
$adapter = new ArrayAdapter($app['your.data.source']);
return new Pagerfanta($adapter);
});
Request-Based Pagination
Use Laravel's Request to dynamically set pagination:
$pagerfanta->setCurrentPage($request->input('page', 1));
$pagerfanta->setMaxPerPage($request->input('per_page', 15));
Adapter Compatibility
DoctrineORMAdapter may not work with DQL COUNT queries in some cases).Pagerfanta\Adapter\DoctrineDBALAdapter for raw SQL queries or ensure your DQL is compatible.Current Page Validation
setCurrentPage() against getNbPages(). Invalid pages may return empty results.$currentPage = max(1, min($pagerfanta->getNbPages(), $request->get('page', 1)));
$pagerfanta->setCurrentPage($currentPage);
Memory Issues with Large Datasets
Pagerfanta\Adapter\CursorAdapter).Zero-Based vs One-Based Indexing
ArrayAdapter) use zero-based indexing internally, while others (e.g., DoctrineORMAdapter) use one-based.Check Adapter Counts
Verify getNbResults() matches your expected dataset size:
$adapter = $pagerfanta->getAdapter();
$totalItems = $adapter->getNbResults();
Log Pagerfanta State Dump the pagerfanta object to debug:
\Log::debug([
'current_page' => $pagerfanta->getCurrentPage(),
'max_per_page' => $pagerfanta->getMaxPerPage(),
'nb_pages' => $pagerfanta->getNbPages(),
'nb_results' => $pagerfanta->getNbResults(),
]);
Custom Adapters
Extend \Pagerfanta\Adapter\AdapterInterface for new data sources (e.g., API responses, Elasticsearch):
class ApiAdapter implements AdapterInterface {
public function getNbResults() { /* ... */ }
public function getSlice($offset, $length) { /* ... */ }
}
Override Pagerfanta Behavior
Extend \Pagerfanta\Pagerfanta to add custom logic (e.g., caching, analytics):
class CustomPagerfanta extends Pagerfanta {
public function getCachedPage($page) { /* ... */ }
}
Integrate with Laravel Collections
Use Pagerfanta\Bridge\Doctrine\ORM\QueryAdapter or wrap collections:
$collection = Post::paginate(10)->getCollection();
$adapter = new ArrayAdapter($collection->all());
setMaxPerPage()).
'pagination' => [
'default_per_page' => 15,
],
Then apply in your code:
$pagerfanta->setMaxPerPage(config('pagination.default_per_page'));
How can I help you explore Laravel packages today?