简体   繁体   English

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

[英]How to paginate table in laravel 5.5?

I'm still new to laravel. 我还是laravel的新手。 I'm trying to include a paginate function here in my app and I find the paginate function of laravel a bit confusing. 我试图在我的应用程序中包含分页功能,但我发现laravel的分页功能有些混乱。 Can Somebody help me with this? 有人可以帮我吗? Here's my code: 这是我的代码:

usertable.blade.php 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 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);

    }

Have a look at the docs over here 看看这里的文档

You can simply append the paginate() function to your query. 您可以简单地将paginate()函数附加到查询中。 As an argument you pass the desired pagination length. 作为参数,您可以传递所需的分页长度。 Fe if you want 15 results returned paginate(15) . Fe如果要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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM