DEV Community

Khandokar Nafis Jaman
Khandokar Nafis Jaman

Posted on

Laravel pagination: serial problem (serial starts with 0)

When you use Laravel default pagination, you may face a problem with its Key. If you use Key as the serial number, when you go to the second page, the key starts from 0.

To fix this, several methods can be applied. There are some build-in function. But these have a version issue. That's why, I can show you a way without using build-in version.

At first, let's see the url.

https://pgnbd.com/registration/view?page=2

This is the url when you are in second page. Similarly, when it's third page, the url is:

https://pgnbd.com/registration/view?page=3

So, we can catch the page parameter into a variable by this.

if(isset($_GET['page']))
$i= $_GET['page']-1;
else
$i = 0;

The else portion is for the first page where you can't find any page parameter. Suppose you show 20 data in a page using paginate like this paginate(20). Then you can write the serial number like this.

@foreach ($gallery as $key=>$item)
{{($key+1) + ($i*20)}}
@endforeach

Happy coding ๐Ÿ˜Ž!!

Top comments (0)