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

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free β†’

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay