Laravel 5.1 自动增加带分页的行号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35489120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:15:53  来源:igfitidea点击:

Laravel 5.1 auto incrementing row number with pagination

phplaravelpagination

提问by Ohidul Islam

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

有没有办法在laravel 5.1中获得漂亮的分页网址或类似的东西?

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

我每页有 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

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?

但是当我进入下一页时, $count var 从头开始​​。我怎样才能得到 $count = 16 并且它会增加等等?

回答by Rody Molenaar

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

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

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

in your blade templates now.

现在在您的刀片模板中。

回答by Mhd Syarif

You can use :

您可以使用 :

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

回答by Jilson Thomas

You can use the paginate helper method:

您可以使用分页助手方法:

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

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

So you can use this formula:

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

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

回答by Shloka

Modify your first line of code

修改你的第一行代码

$count = 1 

by below code,

通过下面的代码,

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

回答by Nitish Mishra

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

回答by Golam Sorwar

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