Laravel Pagination

Pagination is the process of organizing or breaking a large number of records or information which makes it more manageable and easier to navigate.

See Also : How to Install Illuminate/HTML Package In Laravel 5

Laravel Pagination

Laravel Pagination

Building a pagination from scratch can be a nightmare. Fortunately, Laravel makes this process very simple and easy to accomplish with very few lines of code. To build a pagination, you can use paginate method along with the Query builder or with an Eloquent model.

Example for building a simple Laravel pagination is as shown below,

The Controller:

using Query Builder,

public function index()
{
    $users = DB::table('users')->paginate(10);
    return view('users', compact('users '));
}

using Eloquent Model,

public function index()
{
    $users = User::paginate(10);
    return view('users', compact('users '));
}

The parameter passed to paginate method is number of records or items you want to display per page.

The View:

To display paginated links on the view template we need use render method.

@extends('layout.default')
@section('content')
  <div class="container">
    <ul>
      @foreach($users as $user)
        <li> {{ $user->name }} </li>
      @endforeach
    </ul>
  </div>
  {{ $users->links() }}
@stop

Also, the generated HTML output is compatible with Twitter bootstrap.

 

0 Comments

Leave a Comment.