Laravel 5.1分页自动递增行号

4

有没有办法在Laravel 5.1中获得分页的美化URL或类似的东西?

每页显示15行数据。我想即使在分页时也要增加行计数。

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

但是当我跳转到下一页时,$count变量又从头开始计数。 如何让 $count = 16 并且可以依次递增?


$count = ($page-1) * 15 + 1; 对于第一页,$count将为1,对于第二页,$count将为16。 - Amir Bar
它抛出了一个错误。未定义变量:page然后我添加了这个$_GET['page']代替$page。 现在它按照我的预期正常工作。非常感谢您。 - Ohidul Islam
当然可以。我刚刚给了你公式,请用当前页码替换“$page”。 - Amir Bar
7个回答

12

在 Laravel 5.3 中,你可以在视图中使用 $loop 变量。因此,你可以像这样做:

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

现在在您的Blade模板中。


3
你可以使用以下方法:
@foreach ($products as $key=>$val)
  {{ ($products->currentpage()-1) * $products->perpage() + $key + 1 }}
@endforeach

1
您可以使用分页辅助方法: $results->perPage() -> 每页的数量 $results->currentPage() -> 当前页码
因此,您可以使用以下公式:
(($results->currentPage() - 1 ) * $results->perPage() ) + $count

0

您可以像这样获取结果

写入代码:
<?php $i = $years->perPage() * ($years->currentPage() -1); ?>
foreach() 循环开始之前

然后:
<tr>
<td>{{ $i+1 }}</td>
</tr>

最后一步:
endforeach() 之前
</tr> 之后写入 <?php $i++;?>

像这样:
</tr>
<?php $i++;?>
@endforeach


0

修改你的第一行代码

$count = 1 

通过以下代码,

$count = ($years->currentpage() - 1) * $years->perpage();

0
如果你可以使用分页,那么可以尝试这个方法:
@foreach ($noticelist as $key=>$info)
 <tr>
  <th scope="row">{{ ($noticelist->currentpage()-1) * $noticelist->perpage() + $key + 1 }}</th>
  </tr>
@endforeach

0
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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接