laravel show only next and previous link in pagination
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Laravel Pagination: Showing Only Next and Previous Links
Dealing with pagination in web applications is a fundamental task in building scalable interfaces. When developers use Laravel's built-in pagination features, they often want more control over how those navigation links are displayed. Specifically, you want to avoid cluttering your UI with a full list of page numbers (1, 2, 3, etc.) and only show the essential "Next" and "Previous" controls.
This guide will walk you through why the standard approach sometimes falls short and provide the robust solution for displaying only the necessary navigation links using Laravel.
Understanding Standard Pagination Links
When you use methods like links() on a paginator object, Laravel generates HTML based on the pagination data it received. Even when using the simpler simplePaginate(), the underlying mechanism is designed to provide comprehensive navigation options by default. The reason you are still seeing page numbers is that the links() method is designed to render all available links generated by the paginator, regardless of whether you explicitly want to hide the intermediate pages.
The simple pagination trait (simplePaginate) is excellent for performance when you only need the current page data or a simple link structure, but it doesn't inherently strip away the full set of navigation options provided by the underlying Paginator.
// Example showing why links() still outputs more than just next/previous
$users = User::paginate(10);
// This will output links for pages 1, 2, 3...
echo $users->links();The Solution: Manually Constructing Navigation Links
Since the default methods don't offer a direct "show only next/previous" flag, the most effective way to achieve this highly customized UI is to bypass the automatic generation of links() and manually construct the required links using the methods available on your Paginator instance. This gives you complete control over the HTML output.
To do this effectively, you first need to retrieve the paginator object and then use its built-in accessor methods: next() and previous().
Step-by-Step Implementation
- Retrieve the Paginator: Ensure your Eloquent query uses a standard pagination method (like
paginate()) if you need access to these specific navigation pointers. - Check for Next/Previous: Use conditional logic to determine if those links should be displayed based on whether there are subsequent or preceding pages.
Here is an example demonstrating how you can conditionally render the links:
use Illuminate\Support\Facades\Route;
use App\Models\Post;
// Controller method example
public function index()
{
$posts = Post::orderBy('created_at', 'desc')->paginate(5);
// Determine if previous and next links exist
$hasPrevious = $posts->hasPrevious();
$hasNext = $posts->hasNext();
return view('posts.index', [
'posts' => $posts,
'hasPrevious' => $hasPrevious,
'hasNext' => $hasNext,
]);
}Blade View Implementation
In your Blade file, you can now selectively render the links:
{{-- Display Previous Link if it exists --}}
@if ($hasPrevious)
<a href="{{ $posts->previousPageUrl() }}">Previous</a>
@endif
{{-- Display Next Link if it exists --}}
@if ($hasNext)
<a href="{{ $posts->nextPageUrl() }}">Next</a>
@endifBy using previousPageUrl() and nextPageUrl(), you directly generate the correct URLs for navigation without relying on Laravel's general link generation, giving you precise control over your pagination display. This approach is a powerful technique when implementing custom UI patterns within your application, aligning perfectly with the principles of clean, efficient code championed by the Laravel ecosystem.
Conclusion
While Laravel provides powerful tools like links() for standard pagination, achieving highly specialized link displays requires stepping outside the default methods and engaging directly with the Paginator object's specific navigation methods. By checking $posts->hasPrevious() and utilizing previousPageUrl() and nextPageUrl(), you gain full control over your interface, ensuring a cleaner and more intuitive user experience for navigating through large datasets. Always remember that understanding the underlying mechanics of Eloquent and pagination is key to mastering modern web development.