How to increment row number with Laravel pagination

Panjeh
2 min readJul 25, 2019

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.

Before you continue with the rest of this tutorial, I would like to introduce two packages for Laravel that I have recently developed: Laravel Pay Pocket, a modern multi-wallet package, and Laravel Failed Jobs, a UI for the Laravel Failed Jobs Table. I hope they may be of help to you.

https://github.com/HPWebdeveloper/laravel-pay-pocket
https://github.com/HPWebdeveloper/laravel-failed-jobs

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

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

--

--