How to increment row number with Laravel pagination

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

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

Posting daily about Python, Nuclear Physicist PhD

Posting daily about Python, Nuclear Physicist PhD