user2194246
user2194246

Reputation: 203

increment row number with laravel pagination

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

Upvotes: 17

Views: 33214

Answers (18)

Ajowi
Ajowi

Reputation: 505

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.

Upvotes: 0

Tharindu Dissanayake
Tharindu Dissanayake

Reputation: 11

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.

Upvotes: 0

Bahyu Sanciko
Bahyu Sanciko

Reputation: 1

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

using laravel new

Upvotes: 0

Ravi A
Ravi A

Reputation: 1

@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.

Upvotes: 0

Azizullah Saeidi
Azizullah Saeidi

Reputation: 39

$products = Product::paginate(); // ProductController  
    

// product/index.blade.php

@foreach($products as $product)
<tr>
  <td>
    {{($products->currentPage() - 1)  * $products->perPage() + $loop->iteration}}
  </td>
<tr>
@endforeach

Upvotes: 0

u628898
u628898

Reputation: 131

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()

Upvotes: 1

Roby Cigar
Roby Cigar

Reputation: 948

Laravel 8++

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

{{ ($barangs->currentPage() - 1)  * $barangs->count() + $loop->iteration }}

Upvotes: 0

Saeed Awan
Saeed Awan

Reputation: 456

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

Upvotes: 0

Elbo Shindi Pangestu
Elbo Shindi Pangestu

Reputation: 2381

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

Upvotes: 1

Asad Aslam
Asad Aslam

Reputation: 11

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

Upvotes: 1

Alex Gore
Alex Gore

Reputation: 332

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>

Upvotes: 4

Sodruldeen Mustapha
Sodruldeen Mustapha

Reputation: 1271

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

Upvotes: 15

Ashok Barua
Ashok Barua

Reputation: 11

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

@foreach($yourVariable as $item)

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

.....

@php($sl++)

@endforeach

Upvotes: 1

Shweta
Shweta

Reputation: 799

You can simply add the following line

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

in place of

$i = 1;

Upvotes: 1

o.v
o.v

Reputation: 885

In Laravel 5.3 you can use firstItem():

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

Upvotes: 31

Mhd Syarif
Mhd Syarif

Reputation: 101

Laravel 5.3

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

Upvotes: 7

user1429277
user1429277

Reputation: 36

Or avoid php tags completely by

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

Upvotes: 0

Jason Lewis
Jason Lewis

Reputation: 18665

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; ?>

Upvotes: 8

Related Questions