如何在laravel 5.5中对表进行分页?

时间:2018-10-15 05:40:40

标签: 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);

    }

1 个答案:

答案 0 :(得分:4)

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