Laravel Database Pagination


Laravel Database Pagination – The Laravel Database Pagination is provide simple and easy to use pagination of database result out of the box.


Laravel Database Pagination.

Let us understand how to use laravel Database Pagination.

Function:-

There are some followings function available in laravel Database Pagination.

  • 1. Introduction.
  • 2. Basic Usage.
  • 3. Displaying Pagination Results.
  • 4. Customizing The Pagination View.
  • 5. Paginator Instance Methods.

1. Introduction.

It other framework pagination create very hard but in laravel paginator is complete with the query builder and Eloquent ORM. It is provide simple and easy to use pagination of dabase result.
The HTML generated by the paginator is compatible with Bootstrap CSS framework.

2. Basic Usage.

Paginating Query Builder Results

There are many way to create pagination item. The simple way is by using the paginate
method on the query builder or an Eloquent query. This method automatically takes care of setting the proper limit and offset based on the current page being viewed by the user.

Let’s look at a simple example.

<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;

class UserController extends Controller
{
    public function index()
    {
        $users = DB::table('users')->paginate(15);

        return view('user.index', ['users' => $users]);
    }
}

Simple Pagination

If you have to need to display simple Next and Previous link in your pagination view. Then you can use simplePaginate method to perform a more efficient query.

$users = DB::table('users')->simplePaginate(15);

Paginating Eloquent Results

You can also paginate Eloquent queries, like we will paginate the user model with 10 item per page.

$users = DB::table('users')->simplePaginate(10);

You can call paginate after setting other constraints on the query, such as where clauses.

$users = User::where('votes', '>', 100)->paginate(15);

You can also use the simplePaginate method when paginating Eloquent models.

$users = User::where('votes', '>', 100)->simplePaginate(15);

3. Displaying Pagination Results.

You can display the result and render the page links using blade.

<div class="container">
    @foreach ($users as $user)
        {{ $user->name }}
    @endforeach
</div>

{{ $users->links() }}

The links method will render the link to the rest of the page inthe result set.

Customizing The Paginator URI

The withPath method allows you to customize the URI used by the paginator when generating links.

Let’s look at a simple example.

Route::get('users', function () {
    $users = App\User::paginate(15);

    $users->withPath('custom/url');

    //
});

Appending To Pagination Links

You can append to the query string of pagination links using the appemds method.

{{ $users->appends(['sort' => 'votes'])->links() }}

If you want to append a “hash fragment” to the paginator’s URLs, you may use the fragment method.

{{ $users->fragment('foo')->links() }}

Converting Results To JSON

The Laravel pagination result classes implemented the Illuminate/Contracts/Support/Jsonable Interface contract and expose the toJson
method. So it is very simple to convert pagination result to JSON. You can also convert a paginator instance to JSON by simply returning it from a route or controller action.

Route::get('users', function () {
    return App\User::paginate();
});

Let’s look at a simple example.

{
   "total": 50,
   "per_page": 15,
   "current_page": 1,
   "last_page": 4,
   "next_page_url": "http://laravel.app?page=2",
   "prev_page_url": null,
   "from": 1,
   "to": 15,
   "data":[
        {
            // Result Object
        },
        {
            // Result Object
        }
   ]
}

4. Customizing The Pagination View.

You can customize the pagination view like this:-

{{ $paginator->links('view.name') }}

// Passing data to the view...
{{ $paginator->links('view.name', ['foo' => 'bar']) }}

The simplest way to customize the pagination views is by exporting them to your resources/views/vendor directory using the vendor:publish command.

php artisan vendor:publish --tag=laravel-pagination

5. Paginator Instance Methods.

Each paginator instance provides additional pagination information via the following methods.

Laravel Database Pagination

Advertisements

Add Comment

đź“– Read More