【发布时间】:2016-07-18 00:38:02
【问题描述】:
我正在开发一个使用 Laravel 5.2.* 构建的项目。我有一个名为users 的表(与roles 表相关联),该表的模型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>
我不知道我错过了什么。此代码未显示分页。谁能帮我解决这里的问题?
【问题讨论】:
-
只要您的项目少于您的选择,就不会显示任何链接。要测试它,只需使用
paginate(1) -
非常感谢。它现在正在工作。您可以在此处发布它作为答案,以便其他人受益。
标签: php laravel pagination laravel-5.2