Reinhard Lohengramm
Reinhard Lohengramm

Reputation: 75

How to paginate table in laravel 5.5?

I'm still new to laravel. I'm trying to include a paginate function here in my app and I find the paginate function of laravel a bit confusing. Can Somebody help me with this? Here's my code:

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);

    }

Upvotes: 2

Views: 3734

Answers (1)

Tstar
Tstar

Reputation: 518

Have a look at the docs over here

You can simply append the paginate() function to your query. As an argument you pass the desired pagination length. F.e. if you want 15 results returned 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

Upvotes: 5

Related Questions