Reputation: 107
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?
Upvotes: 4
Views: 6490
Reputation: 11
You can get the results like this
Write this:
<?php $i = $years->perPage() * ($years->currentPage() -1); ?>
Before foreach()
loop starts
After that:
<tr>
<td>{{ $i+1 }}</td>
</tr>
Then last step:
Before endforeach()
After </tr>
Write this <?php $i++;?>
Like this:
</tr>
<?php $i++;?>
@endforeach
Upvotes: 0
Reputation: 1615
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
Upvotes: 0
Reputation: 1
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
Upvotes: 0
Reputation: 799
Modify your first line of code
$count = 1
by below code,
$count = ($years->currentpage() - 1) * $years->perpage();
Upvotes: 0
Reputation: 206
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.
Upvotes: 12
Reputation: 101
You can use :
@foreach ($products as $key=>$val)
{{ ($products->currentpage()-1) * $products->perpage() + $key + 1 }}
@endforeach
Upvotes: 3
Reputation: 7313
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
Upvotes: 1