4
votes

Is there any way to get pagination pretty url or something like this in laravel 5.1?

I have 15 rows in per page. And I want to increment the row count number even on paginate.

<?php  $count = 1; ?>
<tr>     
    <th>No</th>
    <th>Year</th>
    <th>Action</th>
</tr>
@foreach($years as $year)
<tr>
    <td width="20%">{{ $count++ }}</td>
    <td width="50%">{{ $year->year }}</td>
    <td width="30%">
        <a href="{{ route('admin.year.edit', $year->id) }}" class="btn btn-info">Edit</a>
    </td>
</tr>
@endforeach

But when I goes to next page the $count var starts from beginning. How can I get $count = 16 and it will increment and so on?

6
$count = ($page-1) * 15 + 1; for for page 1 count will be 1 and for page 2 count will be 16Amir Bar
it threw an error. Undefined variable: page then I have added this $_GET['page'] instead of $page . And its working fine as I expected. Thanks you so muchOhidul Islam
ofcourse.. I just gave you the formula, replace $page with the current page numberAmir Bar

6 Answers

12
votes

In Laravel 5.3 you can now use the $loop variable in your views. So you could do something like:

{{ (($results->currentPage() - 1 ) * $results->perPage() ) + $loop->iteration }}

in your blade templates now.

3
votes

You can use :

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

You can use the paginate helper method:

$results->perPage() -> per page count $results->currentPage() -> current page number

So you can use this formula:

(($results->currentPage() - 1 ) * $results->perPage() ) + $count
0
votes

Modify your first line of code

$count = 1 

by below code,

$count = ($years->currentpage() - 1) * $years->perpage();
0
votes
You can use :

<?php $i = $years->perPage() * ($years->currentPage() -1); ?>

@foreach($years as $year)
<tr>
    <td width="20%">{{ $i }}</td>
    <td width="50%">{{ $year->year }}</td>
    <td width="30%">
        <a href="{{ route('admin.year.edit', $year->id) }}" class="btn btn-info">Edit</a>
    </td>
</tr>
<?php $i++;?>
@endforeach
0
votes

If you can use pagination then try this:

@foreach ($noticelist as $key=>$info)
 <tr>
  <th scope="row">{{ ($noticelist->currentpage()-1) * $noticelist->perpage() + $key + 1 }}</th>
  </tr>
@endforeach