【问题标题】:Laravel 5.1 auto incrementing row number with paginationLaravel 5.1 使用分页自动增加行号
【发布时间】:2016-05-31 01:42:00
【问题描述】:

有没有办法在 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;第 1 页的计数为 1,第 2 页的计数为 16
  • 它抛出了一个错误。未定义的变量: page 然后我添加了这个 $_GET['page'] 而不是 $page 。正如我所料,它工作正常。非常感谢你
  • 当然..我只是给你公式,用当前页码替换$page

标签: php laravel pagination


【解决方案1】:

如果你可以使用分页,那么试试这个:

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

【讨论】:

    【解决方案2】:
    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
    

    【讨论】:

      【解决方案3】:

      修改你的第一行代码

      $count = 1 
      

      通过以下代码,

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

      【讨论】:

        【解决方案4】:

        在 Laravel 5.3 中,您现在可以在视图中使用 $loop 变量。所以你可以这样做:

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

        现在在您的刀片模板中。

        【讨论】:

          【解决方案5】:

          你可以使用:

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

          【讨论】:

            【解决方案6】:

            你可以使用分页辅助方法:

            $results-&gt;perPage() -> 每页计数 $results-&gt;currentPage() -> 当前页码

            所以你可以使用这个公式:

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2013-08-04
              • 1970-01-01
              • 2016-03-16
              • 2016-01-05
              • 2017-05-11
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多