我对 Laravel 还很陌生。我试图在我的应用程序中包含一个分页函数,但我发现 laravel 的分页函数有点令人困惑。有人可以帮我吗?这是我的代码:
usertable.blade.php
<div class="container-fluid col-xs-7 col-md-7">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Update Records </h3>
</div>
<div class="panel-body">
<div class="table-responsive">
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>Username</th>
<th>Name</th>
<th>Department</th>
<th>Action</th>
</tr>
</thead>
<tbody>
@foreach($records as $key=>$record)
<tr>
<td class="col-xs-2">{{$record->name}}</td>
<td class="col-xs-6">{{$record->wholename}} @if($record->isBACSec == 1)<strong>-BAC Secretariat</strong>@endif</td>
<td class="col-xs-2">{{$record->department}}</td>
<td class="col-xs-2"><a class="btn btn-warning btn-xs" href="{{ URL::to('edit/'.$record->id) }}">Edit</a>
<a class="btn btn-danger btn-xs" href="{{ URL::to('delete/'.$record->id) }}" onclick="return confirm('WARNING.'+'\n'+'DELETING USER RECORDS. Continue?');">Delete</a></td>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
{{ $records->links() }}
</div>
</div>
</div>
RecordsController.php
public function edit($id)
{
//find record of given id
$edit_form = User::find($id);
$records = User::all()->paginate(15);
$dept = Office::all();
$result = DB::table('users')
->where('isBACSec', '=', 1)
->get();
//show edit form and pass the info to it
return View('updateuser')
->with('edit_form',$edit_form)
->with('dept',$dept)
->with('records',$records)
->with('result',$result);
}
最佳答案
查看 here 上的文档
您只需将 paginate()
函数附加到查询中即可。
作为参数,您传递所需的分页长度。 F.e.如果您想要返回 15 个结果paginate(15)
。
//find record of given id
$edit_form = User::find($id);
$records = User::all()->paginate(15);
$dept = Office::all();
$result = DB::table('users')
->where('isBACSec', '=', 1)
->paginate(15) // Changes here
关于php - 如何在 Laravel 5.5 中对表格进行分页?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52810327/