DEV Community

Khandokar Nafis Jaman
Khandokar Nafis Jaman

Posted on

1

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 😎!!

Dynatrace image

Observability should elevate – not hinder – the developer experience.

Is your troubleshooting toolset diminishing code output? With Dynatrace, developers stay in flow while debugging – reducing downtime and getting back to building faster.

Explore Observability for Developers

Top comments (0)

MongoDB Atlas runs apps anywhere. Try it now.

MongoDB Atlas runs apps anywhere. Try it now.

MongoDB Atlas lets you build and run modern apps anywhere—across AWS, Azure, and Google Cloud. With availability in 115+ regions, deploy near users, meet compliance, and scale confidently worldwide.

Start Free

👋 Kindness is contagious

Discover more in this insightful article and become part of the thriving DEV Community. Developers at every level are welcome to share and enrich our collective expertise.

A simple “thank you” can brighten someone’s day. Please leave your appreciation in the comments!

On DEV, sharing skills lights our way and strengthens our connections. Loved the read? A quick note of thanks to the author makes a real difference.

Count me in