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

user2194246
  • 193
  • 1
  • 1
  • 6

16 Answers16

29

In Laravel 5.3 you can use firstItem():

@foreach ($items as $key => $value)    
  {{ $items->firstItem() + $key }}
@endforeach
o.v
  • 835
  • 10
  • 15
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
Sodruldeen Mustapha
  • 1,135
  • 10
  • 22
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; ?>
Jason Lewis
  • 18,537
  • 4
  • 61
  • 64
7

Laravel 5.3

@foreach ($products as $key=>$val)
  {{ ($products->currentpage()-1) * $products->perpage() + $key + 1 }}
@endforeach
Mhd Syarif
  • 101
  • 1
  • 2
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>
Alex Gore
  • 332
  • 1
  • 3
  • 9
1

You can simply add the following line

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

in place of

$i = 1;
Shweta
  • 661
  • 6
  • 11
1
@php($sl = ($yourVariable->perPage() * $yourVariable->currentPage()) - ($yourVariable->perPage() - 1))

@foreach($yourVariable as $item)

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

.....

@php($sl++)

@endforeach
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
parisssss
  • 803
  • 16
  • 36
Asad Aslam
  • 11
  • 1
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

Elbo Shindi Pangestu
  • 2,021
  • 2
  • 12
  • 24
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()

u628898
  • 121
  • 1
  • 4
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

Saeed Awan
  • 416
  • 6
  • 8
0

Laravel 8++

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

{{ ($barangs->currentPage() - 1)  * $barangs->count() + $loop->iteration }}
Roby Cigar
  • 766
  • 2
  • 14
  • 28
  • `$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 '22 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
  • 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 '23 at 03: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.

tripleee
  • 175,061
  • 34
  • 275
  • 318
Ravi A
  • 1
  • 1
0
@php
 $i = ($users->currentPage() - 1) * $users->perPage() + 1;
@endphp

using laravel new