使用 Laravel 分页增加行号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18024237/
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 08:13:21  来源:igfitidea点击:

increment row number with laravel pagination

laravellaravel-3

提问by user2194246

How to make increment row number with laravel pagination ? When i use pagination and i go to page 2 and above it will back to beginning. for example i will paginate(3)

如何使用 Laravel 分页增加行号?当我使用分页并转到第 2 页及以上时,它将返回到开头。例如我会分页(3)

    <thead>
    <tr>
       <th>No</th>
       <th>Name</th>
    </tr>
    </thead>
    <tbody>
    <?php $i = 1; ?>
    @foreach ($telephone->results as $telp)
    <tr>
       <td>
         {{$i++}}
       </td>
       <td>{{ $telp->name }}</td>                               
    </tr>
    @endforeach
    </tbody>

when i go to page 2 the number will start from 1 again.

当我转到第 2 页时,数字将再次从 1 开始。

i need to make it when i go to page 2 it will start from 4

当我转到第 2 页时,我需要完成它,它将从第 4 页开始

采纳答案by Jason Lewis

You should be able to use the getFrommethod to get the starting number of the current pages results. So instead of setting $i = 1;you should be able to do this.

您应该能够使用该getFrom方法获取当前页面结果的起始编号。因此,$i = 1;您应该能够做到这一点,而不是设置。

<?php $i = $telephone->getFrom(); ?>


In Laravel 3 there is no getFrommethod so you need to calculate it manually.

在 Laravel 3 中没有getFrom方法,所以你需要手动计算它。

<?php $i = ($telephone->page - 1) * $telephone->per_page + 1; ?>

回答by o.v

In Laravel 5.3 you can use firstItem():

在 Laravel 5.3 中你可以使用firstItem()

@foreach ($items as $key => $value)    
  {{ $items->firstItem() + $key }}
@endforeach

回答by Sodruldeen Mustapha

The below works with laravel 5.4

以下适用于laravel 5.4

<?php $i = ($telephone->currentpage()-1)* $telephone-
>perpage() + 1;?>
@foreach($telephone as $whatever)
<td> {{ $i++ }}</td>
@endforeach

EditedThe below works for laravel 5.7 above

编辑以下适用于上面的laravel 5.7

@foreach ($telephone as $key=> $whatever)
  <td>{{ $key+ $telephone->firstItem() }}</td>
@endforeach

回答by Mhd Syarif

Laravel 5.3

Laravel 5.3

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

回答by Ashok Barua

@php($sl = ($yourVariable->perPage() * $yourVariable->currentPage()) - ($yourVariable->perPage() - 1))

@foreach($yourVariable as $item)

<td>{{ $item->key_name }}</td>

.....

@php($sl++)

@endforeach

回答by Alex Gore

For Laravel 6.2: $loop - just in case, is a built-in instance in Blade

对于 Laravel 6.2:$loop - 以防万一,是 Blade 中的一个内置实例

@foreach($array as $item)
    <tr class="table-row">
      <td class="site-id">
         {{($array->currentPage() - 1) * $array->perPage() + $loop->iteration}}
      </td>
    </tr>
@endforeach
</table>

回答by Asad Aslam

In Laravel 6

在 Laravel 6


@php $i = ($data->currentpage()-1)* $data->perpage() + 1;@endphp

@foreach($data as $banner)
 <tr>
   <td>{{$i}}</td>
   <td><img src="{{ URL::to('/') }}/dist/img/{{$banner->name}}" height="250" width="600"></td>
 </tr>
 @php  $i += 1; @endphp
@endforeach

回答by Shloka

You can simply add the following line

您可以简单地添加以下行

$i = ($telephone->currentpage()-1)* $telephone->perpage();

in place of

代替

$i = 1;

回答by user1429277

Or avoid php tags completely by

或者完全避免 php 标签

 @foreach ($users as $key => $user)
    {{ (Input::get('page', 1) - 1) * $users->getPerPage() + $key + 1 }}
 @endforeach

回答by Elbo S.P.

If you are using Laravel Pagination. It works very well

如果您使用 Laravel 分页。它运作良好

The backend

后端

public function index()
{
    return view('your.telephone.index', [
        'telephone' => Telephone::paginate(15)
    ]);
}

Blade front end

刀片前端

<td>{{ (($telephone->currentPage() * 10) - 10) + $loop->iteration  }}</td>

And don't forget embed the page

并且不要忘记嵌入页面

{{ $telephone->links() }}

For $loop->iteration

为了 $loop->iteration

See the doc here: https://laravel.com/docs/7.x/blade#the-loop-variable

请参阅此处的文档:https: //laravel.com/docs/7.x/blade#the-loop-variable

For $telephone->currentPage()

为了 $telephone->currentPage()

See the doc here: https://laravel.com/docs/7.x/pagination#paginator-instance-methods

请参阅此处的文档:https: //laravel.com/docs/7.x/pagination#paginator-instance-methods