Using Laravel’s pagination. It is working but when it gets to the last page it shows both the previous and next button. Is there a way to hid the "next" button?
public function index() {
$posts = Post::orderBy('created_at', 'desc')->paginate(10);
return view('posts.index', compact('posts'));
}
@foreach ($posts as $post)
{{$post->title}}
@endforeach
{!! $posts->links() !!}
{{ $posts->onEachSide(2)->links() }}
2
Answers
Yes, you can hide the "next" button on the last page in Laravel pagination by customizing the pagination links. Laravel’s default pagination views provide a way to conditionally display the next and previous buttons. You can create a custom pagination view and modify it to hide the "next" button on the last page.
Open the resources/views/vendor/pagination/default.blade.php file (or another view file if you’re using a different style).
Locate the section that renders the "Next" button and add a condition to check if it’s the last page.
3. Use the Custom Pagination View
{!! $posts->links(‘vendor.pagination.default’) !!}
Just use CSS to hide it because on the first page and last page you can’t click on it.
Based on Laravel template default Tailwind CSS framework. For example:
If you want to correct exactly the CSS selector, you should create a custom pagination view