Php Laravel 5.1带分页的自动递增行号
在Laravel5.1中,有没有任何方法可以获得分页漂亮的url或类似的内容 我每页有15行。我想在paginate上增加行计数Php Laravel 5.1带分页的自动递增行号,php,laravel,pagination,Php,Laravel,Pagination,在Laravel5.1中,有没有任何方法可以获得分页漂亮的url或类似的内容 我每页有15行。我想在paginate上增加行计数 <?php $count = 1; ?> <tr> <th>No</th> <th>Year</th> <th>Action</th> </tr> @foreach($years as $year) <tr>
<?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
不
年
行动
@foreach($年为$年)
{{$count++}
{{$year->year}
@endforeach
但是,当我转到下一页时,$count变量从头开始。如何获得$count=16并使其递增,依此类推?您可以使用paginate helper方法:
$results->perPage()
->每页计数
$results->currentPage()
->当前页码
所以你可以用这个公式:
(($results->currentPage() - 1 ) * $results->perPage() ) + $count
您可以使用:
@foreach ($products as $key=>$val)
{{ ($products->currentpage()-1) * $products->perpage() + $key + 1 }}
@endforeach
在Laravel 5.3中,现在可以在视图中使用
$loop
变量。所以你可以这样做:
{{ (($results->currentPage() - 1 ) * $results->perPage() ) + $loop->iteration }}
现在在您的刀片模板中。修改您的第一行代码
$count = 1
根据下面的代码
$count = ($years->currentpage() - 1) * $years->perpage();
您可以使用:
@foreach($年为$年)
{{$i}
{{$year->year}
@endforeach
如果可以使用分页,请尝试以下操作:
@foreach ($noticelist as $key=>$info)
<tr>
<th scope="row">{{ ($noticelist->currentpage()-1) * $noticelist->perpage() + $key + 1 }}</th>
</tr>
@endforeach
@foreach($noticelist as$key=>$info)
{{($noticelist->currentpage()-1)*$noticelist->perpage()+$key+1}}
@endforeach
$count=($page-1)*15+1;对于第1页,计数将为1,对于第2页,计数将为16。它抛出了一个错误。未定义变量:page然后我添加了这个$u GET['page'],而不是$page。正如我所预料的那样,它工作得很好。非常感谢你,当然。。我刚刚给了你一个公式,用当前页码替换$page
@foreach ($noticelist as $key=>$info)
<tr>
<th scope="row">{{ ($noticelist->currentpage()-1) * $noticelist->perpage() + $key + 1 }}</th>
</tr>
@endforeach