Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

meglo's avatar
Level 1

about $loop->iteration and paginate

i already doing paginate(4)

and doing @foreach() $loop->iteration @endforeach

i want column No continue if i change to next page (paginate),

the detail code

<th scope="col">No</th>
@foreach ($attend as $data)
<tr>
<th scope="row">{{ $loop->iteration }}</th>
...

my controller

$attend = Absensi::Paginate(4);
return view('attend.home',['attend'=>$attend]);
0 likes
17 replies
Sinnbeck's avatar

Do you mean like

<th scope="row">{{ $loop->iteration * $attend->currentPage() }}</th>
meglo's avatar
Level 1

if i click next page . the no contiune number 1,2,3,4,5

image

next page start from 2 it should be no 5,6,7,..

image

Sinnbeck's avatar

Ah so something like this

<th scope="row">{{ $loop->iteration + (($attend->currentPage() -1) * $attend->perPage())  }}</th>
1 like
rodrigo.pedra's avatar
Level 56

As you are using the built-in paginator the first index can be retrieved from the ->firstItem() method:

<th scope="col">No</th>
@foreach ($attend as $data)
<tr>
<th scope="row">{{ $attend->firstItem() + $loop->index }}</th>
...
4 likes
rodrigo.pedra's avatar

Thanks @sinnbeck !

It happens that I used that this other day. Sometimes I doubt one can know all API by heart. Good for us Laravel docs are top-notch

Sinnbeck's avatar

Indeed. I remember alot, but not all. And some things I simply dont know :p

meglo's avatar
Level 1

thank you for respon @michaloravec i really appreciate it. i will give you love tags.

@sinnbeck i just choose who reply the first my question. hehehe and you the first person. if it not work it i will choose @michaloravec . bytheway are you getting some reward maybe to answer many question in this forum? i mean real reward..

MichalOravec's avatar

When I saw reply by @sinnbeck I knew that is wrong, so I added my own reply but I really didn't know that something like that is in Laravel by default, what was provided by @rodrigo.pedra

1 like
Sinnbeck's avatar

Yeah I misunderstood what you meant, so my first answer was way off.

Currently I just help out for fun. It is great helping out others who are struggling and it is a great way to give back to the community :)

Sinnbeck's avatar

@michaloravec Well just are right at my heels, so I think it just a matter of time before you get there ;)

Please or to participate in this conversation.