How to increment row number with Laravel pagination

Image for post
Image for post

If you look at the Laravel document (Paginator Instance Methods) here

it says:

$results->firstItem()

Get the result number of the first item in the results.

In another mean,

$results->firstItem()

will get the result id of the first item in the results.

You can see its function definition here. It says : Get the number of the first item in the slice.

Example:

If you pass to the view something like this:

$users = User::all()->paginate(3);
return view('/', compact('users'));

You can generate rows while incrementing rows number with Laravel pagination using such part of code:

@foreach($users as $key => $user)
<tr>
<td>{{ $users->firstItem() + $key }}</td>
<td>{{ $user->name }}</td>
<td>{{ $user->email }}</td>
</tr>
@endforeach
Image for post
Image for post

For a better view:

Don’t forget to clap for this article if you gained something. And share if you care about other developers in the community.

You may also want to know how to increment Laravel eloquent model with or without updating timestamps. Read it here

Image for post
Image for post

Written by

Web geek, Self-taught full-stack web developer, Learning Python, Laravel, Vuejs, UX/UI design, Nuclear Physicist PhD

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store