increment row number with laravel pagination
Asked Answered
V

18

17

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

Vela answered 2/8, 2013 at 18:57 Comment(0)
S
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; ?>
Summon answered 3/8, 2013 at 2:31 Comment(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()Vela
Ah sorry, assumed L4. I've updated my answer to include an L3 variation.Summon
U
31

In Laravel 5.3 you can use firstItem():

@foreach ($items as $key => $value)    
  {{ $items->firstItem() + $key }}
@endforeach
Underbrush answered 7/12, 2016 at 15:45 Comment(0)
I
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
Ionone answered 20/9, 2017 at 7:38 Comment(0)
S
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; ?>
Summon answered 3/8, 2013 at 2:31 Comment(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()Vela
Ah sorry, assumed L4. I've updated my answer to include an L3 variation.Summon
E
7

Laravel 5.3

@foreach ($products as $key=>$val)
  {{ ($products->currentpage()-1) * $products->perpage() + $key + 1 }}
@endforeach
Ere answered 7/9, 2016 at 8:48 Comment(0)
M
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>
Mancino answered 9/11, 2019 at 21:46 Comment(0)
H
1

You can simply add the following line

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

in place of

$i = 1;
Heterosis answered 3/8, 2017 at 6:19 Comment(0)
C
1
@php($sl = ($yourVariable->perPage() * $yourVariable->currentPage()) - ($yourVariable->perPage() - 1))

@foreach($yourVariable as $item)

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

.....

@php($sl++)

@endforeach
Calfskin answered 20/3, 2018 at 3:27 Comment(1)
you should explain the answerStale
D
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
Dissatisfaction answered 15/1, 2020 at 10:23 Comment(0)
A
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

Aerate answered 4/5, 2020 at 9:4 Comment(0)
H
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()

Homozygous answered 6/1, 2022 at 7:51 Comment(0)
S
0

Or avoid php tags completely by

 @foreach ($users as $key => $user)
    {{ (Input::get('page', 1) - 1) * $users->getPerPage() + $key + 1 }}
 @endforeach
Scrubber answered 12/11, 2015 at 14:2 Comment(0)
A
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

Abrade answered 28/5, 2021 at 6:50 Comment(0)
S
0

Laravel 8++

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

{{ ($barangs->currentPage() - 1)  * $barangs->count() + $loop->iteration }}
Splendent answered 14/9, 2021 at 3:47 Comment(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.Usia
V
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
Vender answered 15/5, 2023 at 16:0 Comment(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.Koniology
W
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.

Whitt answered 26/5, 2023 at 8:7 Comment(0)
B
0
@php
 $i = ($users->currentPage() - 1) * $users->perPage() + 1;
@endphp

using laravel new

Bethelbethena answered 14/6, 2023 at 9:4 Comment(0)
E
0

This is a simple solution for this issue.

@foreach ($items as $key => $item)
    <tr>
       <td>
         {{ $items->firstItem() + $key}}
       </td> 
       <td>
         {{ $item->name}}
       </td>                                     
    </tr>
@endforeach

Please notice that $items is used to calculate row number, not $item.

Effect answered 19/6 at 13:12 Comment(0)
T
0

To properly set the numbering for paginate results. Simply be aware that the object returned (result set) has some ready to use methods that you can call and manipulate as you desire. Also, there is a keyword loop with its method iteration that can give you simple numbering for most results sets (not just paginate ones).

Now, armed with these, let us see how continuous numbering can be achieved

 @foreach ($users as $user)
    {{ $loop->iteration + (($users->currentPage() - 1) * $users->perPage()) }}
 @endforeach

Explanation: for proper ordinary numbering, we have used reserved keywords $loop->iteration; next we used the paginate methods currentPage() and perPage(). The mathematics aspect am sure you will figure out. This was tested on Laravel 11.x.

Tsui answered 20/7 at 14:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.