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

时间:2016-03-30 06:45:28

标签: php laravel pagination laravel-5.2

我正在开发一个使用Laravel 5.2构建的项目。*。我有一个名为users的表(与roles表相关联)和表{model> 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>

我不知道我错过了什么。此代码未显示分页。任何人都可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:2)

只要您的项目少于您的选择,就不会显示任何链接。

要测试它只需使用:

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

答案 1 :(得分:0)

你应该做这样的事情 -

<强>控制器 -

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