php - 如何在 Laravel 5.5 中对表格进行分页?

标签 php laravel

我对 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/

相关文章:

mysql - 如何检查MySQL中的每一行(时间戳 - 60秒)以确定是否存在重复数据?

php - 将 URL 转换为 SEO 友好

PHP 日期循环库?

javascript - 从 JQuery 向 PHP 发送数据的问题

jquery - 如何返回登录凭据出错的选项卡

php - 使用 phpunit 时, Eloquent 查询范围返回构建器而不是模型

php - 通过 OAuth 将 Google 联系人导入到 PHP 中的 Mysql

php - CSS 未在 Woocommerce Wordpress 模板文件中执行

excel - 使用 laravel excel 和 laravel 队列导入大型 excel 文件

Laravel 4 资源 Controller - 索引操作的附加参数