Skip to content

Instantly share code, notes, and snippets.

@prodeveloper
Created September 18, 2016 06:54
Show Gist options
  • Select an option

  • Save prodeveloper/bae1c9a7c696ffae909c9c02be608887 to your computer and use it in GitHub Desktop.

Select an option

Save prodeveloper/bae1c9a7c696ffae909c9c02be608887 to your computer and use it in GitHub Desktop.
Pagination using laravel

Once you have your app in production. Data is guaranteed to start flowing in. As such you can no longer show all the information from a particular table at once.

Laravel provides an out of the box solution for pagination.

##Step 1:

Ensure you have enough student records so that they can be paginated. That is have at least 20 records in your students database.

You can do this by adding records using your form.

##Step 2

Previously we simply returned all the records from the table

    function getAll(){
         return view('students',['students'=>App\Student::all()]);
    }

This snippet is part of the student controller class.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App;

class StudentController extends Controller
{
    function getAll(){
         return view('students',['students'=>App\Student::paginate(10)]);
    }
    function getAdd(){
        return view('new_student');
    }
    function postAdd(){
             //Create a new student object as we did in the tinker terminal
        $student = new App\Student();
        $student->name= Request::get('name');
        $student->email= Request::get('email');
        $student->course= Request::get('course');
        $student->save();
        //Once saved redirect back to all students table
        return Redirect::to('/students');
    }
    function getEdit($id){
        $student=App\Student::findOrFail($id);
        return view('edit_student',['student'=>$student]);
    }
    function postEdit(){
        $student=App\Student::findOrFail($id);
        $student->name= Request::get('name');
        $student->email= Request::get('email');
        $student->course= Request::get('course');
        $student->save();
        return Redirect::to('/students');
    }
    function getRegistered(){
        return view("registered",['students'=>App\Student::all()]);
    }
    
}

Now instead return the paginator as shown below.

  function getAll(){
         return view('students',['students'=>App\Student::paginate(10)]);
    }

Here we have simply told laravel to paginate with each page having maximum of 10 records.

Refresh your students/all url to confirm.

##Step 3

While we not have 10 records per page, we have no way of accessing the other pages.

To be able to see them we must then show the links on our view.

Lets edit the students.blade.php file

Our original view file looks like this

    <body>
        <table class="table">
            <tr>
                <th>Name</th>
                 <th>Email</th>
                 <th>Course</th>
                  <th>Edit</th>
            </tr>
            @foreach($students as $student)
            <tr>
                <td>{{$student->name}}</td>
                <td>{{$student->email}}</td>
                <td>{{$student->course}}</td>
                <td><a href="{{'students/'.$student->id.'/edit'}}">Edit</a></td>
            </tr>
            @endforeach
        </table>
    </body>

Now we add the link generator {{$students->links}} Note that its $students NOT $student

<body>
        <table class="table">
            <tr>
                <th>Name</th>
                 <th>Email</th>
                 <th>Course</th>
                  <th>Edit</th>
            </tr>
            @foreach($students as $student)
            <tr>
                <td>{{$student->name}}</td>
                <td>{{$student->email}}</td>
                <td>{{$student->course}}</td>
                <td><a href="{{'students/'.$student->id.'/edit'}}">Edit</a></td>
            </tr>
            @endforeach
        </table>
        {{$students->links()}}
    </body>

You should now see the links at the bottom of the page. Laravel will take care of the rest

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment