分页在 Laravel 5.2 中无法正常工作

Pagination is not working properly in Laravel 5.2

我正在开发一个使用 Laravel 5.2.* 构建的项目。我有一个名为 users 的 table(与 roles table 关联)和 table 的模型 User。我想用分页显示用户列表。我的代码如下。

在控制器中:

public function index()
{
    $page_title = "Manage User";

    $roles = Role::where('status', 1)->get();

    $users = User::where('status', 1)->paginate(10);

    return view('users.index', compact('page_title', 'users', 'roles'));
}

users变量的数据格式如下

LengthAwarePaginator {#199 ▼
  #total: 3
  #lastPage: 1
  #items: Collection {#202 ▼
    #items: array:3 [▼
      0 => User {#203 ▶}
      1 => User {#204 ▶}
      2 => User {#205 ▶}
    ]
  }
  #perPage: 10
  #currentPage: 1
  #path: "http://localhost/dtrdimen/UsersList"
  #query: []
  #fragment: null
  #pageName: "page"
}

在查看文件中:

    <table class="table table-bordered table-hover">
        <tr>
            <th style="text-align: center;" width="10%">Id</th>
            <th style="text-align: center;" width="30%">Name</th>
            <th style="text-align: center;" width="15%">Email</th>
            <th style="text-align: center;" width="15%">Role</th>
            <th style="text-align: center;" width="10%">Status</th>
            <th style="text-align: center;" width="20%" colspan="3">Actions</th>
        </tr>

        @foreach ($users as $user)
            <tr>
                <td>{{ $user->id }}</td>
                <td>{{ $user->name }}</td>
                <td>{{ $user->email }}</td>
                <td>{{ $user->role_id }}</td>
                <td>{{ $user->status }}</td>

                <td style="">

                    <a href="/editUser/{{ $user->id }}" class="btn btn-warning pull-left", style="margin-right: 5px;">Edit</a>

                    {!! Form::open(['method' => 'delete', 'route' => ['User.remove', $user->id], 'class' => 'pull-left']) !!}
                    {!! Form::submit('Delete', ['class' => 'btn btn-danger', 'onClick' => "return check()"]) !!}
                    {!! Form::close() !!}
                </td>
            </tr>
        @endforeach
    </table>
    <div class="text-center">
        {!! $users->links() !!} //I also used render() instead of links
    </div>

我不知道我错过了什么。此代码未显示分页。谁能帮我解决这里的问题?

你应该做这样的事情-

控制器-

public function paging() {
    $users = User::paginate(10);
    return View::make('paging', compact('users'));
}

查看-

@extends('layout.default')
@section('content')
<div class="container">
  <table width="60%" cellspacing="0" cellpadding="4" border="0" class="data">
    @foreach($users as $user)
    <tr>
      <td> {{ $user->u_firstname }} </td>
      <td> {{ $user->u_lastname }} </td>
      <td> {{ $user->u_email }} </td>
    </tr>
    @endforeach
  </table>
  <div class="pagination"> {{ $users->links() }} </div>
</div>
@stop

路线-

Route::get('account/paging', 'AccountController@paging');

提供了创建分页的教程 -

http://learn24bd.com/laravel-5-tutorial-06-pagination-in-laravel-5/

http://tutsnare.com/how-to-create-pagination-in-laravel/

这里给出了文档-

https://laravel.com/docs/5.1/pagination

只要您的项目少于您的选择,就不会显示 link。

要测试它,只需使用:

$users = User::where('status', 1)->paginate(1);