16

How to make increment row number with laravel pagination ? When i use pagination and i go to page 2 and above it will back to beginning. for example i will paginate(3)

    <thead>
    <tr>
       <th>No</th>
       <th>Name</th>
    </tr>
    </thead>
    <tbody>
    <?php $i = 1; ?>
    @foreach ($telephone->results as $telp)
    <tr>
       <td>
         {{$i++}}
       </td>
       <td>{{ $telp->name }}</td>                               
    </tr>
    @endforeach
    </tbody>

when i go to page 2 the number will start from 1 again.

i need to make it when i go to page 2 it will start from 4

16 Answers 16

30

In Laravel 5.3 you can use firstItem():

@foreach ($items as $key => $value)    
  {{ $items->firstItem() + $key }}
@endforeach
0
15

The below works with laravel 5.4

<?php $i = ($telephone->currentpage()-1)* $telephone-
>perpage() + 1;?>
@foreach($telephone as $whatever)
<td> {{ $i++ }}</td>
@endforeach

Edited The below works for laravel 5.7 above

@foreach ($telephone as $key=> $whatever)
  <td>{{ $key+ $telephone->firstItem() }}</td>
@endforeach
0
8

You should be able to use the getFrom method to get the starting number of the current pages results. So instead of setting $i = 1; you should be able to do this.

<?php $i = $telephone->getFrom(); ?>

In Laravel 3 there is no getFrom method so you need to calculate it manually.

<?php $i = ($telephone->page - 1) * $telephone->per_page + 1; ?>
2
  • Thank you for your kind attention. By the way when I use getForm method i get error Call to undefined method Laravel\Paginator::getFrom() Aug 3, 2013 at 15:12
  • Ah sorry, assumed L4. I've updated my answer to include an L3 variation. Aug 4, 2013 at 2:49
7

Laravel 5.3

@foreach ($products as $key=>$val)
  {{ ($products->currentpage()-1) * $products->perpage() + $key + 1 }}
@endforeach
4

For Laravel 6.2: $loop - just in case, is a built-in instance in Blade

@foreach($array as $item)
    <tr class="table-row">
      <td class="site-id">
         {{($array->currentPage() - 1) * $array->perPage() + $loop->iteration}}
      </td>
    </tr>
@endforeach
</table>
1

You can simply add the following line

$i = ($telephone->currentpage()-1)* $telephone->perpage();

in place of

$i = 1;
1
@php($sl = ($yourVariable->perPage() * $yourVariable->currentPage()) - ($yourVariable->perPage() - 1))

@foreach($yourVariable as $item)

<td>{{ $item->key_name }}</td>

.....

@php($sl++)

@endforeach
1
  • you should explain the answer Mar 20, 2018 at 3:48
1

In Laravel 6


@php $i = ($data->currentpage()-1)* $data->perpage() + 1;@endphp

@foreach($data as $banner)
 <tr>
   <td>{{$i}}</td>
   <td><img src="{{ URL::to('/') }}/dist/img/{{$banner->name}}" height="250" width="600"></td>
 </tr>
 @php  $i += 1; @endphp
@endforeach
1

If you are using Laravel Pagination. It works very well

The backend

public function index()
{
    return view('your.telephone.index', [
        'telephone' => Telephone::paginate(15)
    ]);
}

Blade front end

<td>{{ (($telephone->currentPage() * 10) - 10) + $loop->iteration  }}</td>

And don't forget embed the page

{{ $telephone->links() }}

For $loop->iteration

See the doc here: https://laravel.com/docs/7.x/blade#the-loop-variable

For $telephone->currentPage()

See the doc here: https://laravel.com/docs/7.x/pagination#paginator-instance-methods

1

I'm using this, in Laravel 8, accessing $loop, and links()->paginator:

       @foreach ($users as $user)
            <tr>
                <td class="text-center">{{ ($users->currentPage() - 1)  * $users->links()->paginator->perPage() + $loop->iteration }}</td>
                <td class="text-center">{{$user->name}}</td>
            </tr>
        @endforeach

Which works properly even when the final page is only partially filled.

Could also use $users->perPage() in place of $users->links()->paginator->perPage()

0

Or avoid php tags completely by

 @foreach ($users as $key => $user)
    {{ (Input::get('page', 1) - 1) * $users->getPerPage() + $key + 1 }}
 @endforeach
0

You can use it in your controller. example given below

$records = Table::paginate(20);
return view('yourview',compact('records')->with('i', ($request->input('page', 1) - 1) * 20);

Note: paginate value must be equal to with() function vlaue

after that you can use $i variable in you blade file. controller calculate value according to page number and return to blade

In blade file. use inside the loop

{{ ++$i }}

Hopefully this will help you

0

Laravel 8++

$loop->iteration is available out of the box inside loop.

{{ ($barangs->currentPage() - 1)  * $barangs->count() + $loop->iteration }}
1
  • $barangs->count() should be $barangs->perPage(). count() is for the current page, which will cause issues if you're on the last page and it has a different number of results from the others pages.
    – anjama
    Mar 30, 2022 at 14:04
0
$products = Product::paginate(); // ProductController  
    

// product/index.blade.php

@foreach($products as $product)
<tr>
  <td>
    {{($products->currentPage() - 1)  * $products->perPage() + $loop->iteration}}
  </td>
<tr>
@endforeach
1
  • Add some explanation with code, so that it can help others too for the same or similar problem. A comment has been added as part of the review.
    – B25Dec
    May 16 at 3:03
0
@foreach($showUserData as $key => $data) {
 <tr>
 <td>{{$showUserData->firstItem() + $key}}</td>
</tr>
}
@endforeach

This will give you the sequence you need. It starts from 1 and on next page from the last record +1.

0
@php
 $i = ($users->currentPage() - 1) * $users->perPage() + 1;
@endphp

using laravel new

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.