Get "PHP 8 in a Nuthshell" (Now comes with PHP 8.3)
Amit Merchant

Amit Merchant

A blog on PHP, JavaScript, and more

Checking if the user is on the last page of pagination in Laravel 8.x

Up until now, when you’re working with Laravel’s paginator and if you wanted to check whether the user is on the first page of the pagination in a Blade file, you would be using the onFirstPage() method like so.

@if($paginiator->onFirstPage())

But when you want to check if the user is on the last page, there isn’t a straightforward method to do so. You would need to use the “not so obvious” hasMorePages() method like so.

@if(!$paginiator->hasMorePages())

That’s slightly odd since you’re not using a method that doesn’t sound like its intended purpose and you’re kind of reversing the logic.

That’s where this PR comes into the picture.

The onLastPage method

This PR now introduces a new onLastPage() method that lets you check if the user is on the last page of the pagination like so.

@if($paginiator->onLastPage())

As you can tell, this method sounds exactly like what you’re intending to do and it’s more consistent with the other method onFirstPage().

Learn the fundamentals of PHP 8 (including 8.1, 8.2, and 8.3), the latest version of PHP, and how to use it today with my new book PHP 8 in a Nutshell. It's a no-fluff and easy-to-digest guide to the latest features and nitty-gritty details of PHP 8. So, if you're looking for a quick and easy way to PHP 8, this is the book for you.

Like this article? Consider leaving a

Tip

👋 Hi there! I'm Amit. I write articles about all things web development. You can become a sponsor on my blog to help me continue my writing journey and get your brand in front of thousands of eyes.

Comments?