如何在分页栏中设置数字?

时间:2017-04-20 07:33:50

标签: php laravel pagination laravel-5.3 laravel-eloquent

我在控制器中的查询是这样的:

 $users = DB::table('users')
           ->paginate(10);

我的观点是这样的:

<table>
    <thead>
        <th>Number</th>
        <th>Usename</th>
        <th>Email</th>
        ...
    </thead>
    <tbody>
    @foreach($users as $key => $user)
        <tr>
            <td>{!! ++$key !!}</td>
            <td>{!! $user->username !!}</td>
            <td>{!! $user->email !!}</td>
            ...
        </tr>
    @endforeach
    {{ $users->links() }}
    </tbody>
</table>

它有效

但是,

例如我有15个数据

在第1页上,数字列如下:

  

1

     

2

     

...

     

9

     

10

当我点击第2页时,数字列如下:

  

1

     

2

     

3

     

4

     

5

我想在点击第2页时,数字栏显示如下:

  

11

     

12

     

13

     

14

     

15

我该怎么做?

1 个答案:

答案 0 :(得分:2)

我认为您的代码需要更新如下:

<table>
   <thead>
       <th>Number</th>
       <th>Usename</th>
       <th>Email</th>
       ...
   </thead>
   <tbody>
   @foreach($users as $key => $user)
       <tr>
           <td>{{ ($users->currentpage()-1) * $users->perpage() + $key + 1 }}</td>
           <td>{!! $user->username !!}</td>
           <td>{!! $user->email !!}</td>
           ...
       </tr>
   @endforeach
   {{ $users->links() }}
   </tbody>
</table>

希望这对你有用!