Laravel

Laravel TutorialChange default routing behaviour in Laravel 5.2.31 +CustomException class in LaravelDeploy Laravel 5 App on Shared Hosting on Linux ServerLaravel ArtisanLaravel AuthenticationLaravel AuthorizationLaravel Blade TemplatesLaravel CashierLaravel CollectionsLaravel Common Issues & Quick FixesLaravel ConstantsLaravel ControllersLaravel Cron basicsLaravel Cross Domain RequestLaravel Custom Helper functionLaravel DatabaseLaravel Database MigrationsLaravel Database SeedingLaravel Directory StructureLaravel DockerLaravel EloquentLaravel Eloquent : RelationshipLaravel Eloquent: Accessors & MutatorsLaravel Eloquent: ModelLaravel Error HandlingLaravel Events and ListenersLaravel Filesystem / Cloud StorageLaravel Form Request(s)Laravel Getting started with laravel-5.3Laravel HelpersLaravel HTML and Form BuilderLaravel InstallationLaravel Installation GuideLaravel Introduction to laravel-5.2Laravel Introduction to laravel-5.3Laravel lumen frameworkLaravel Macros in Eloquent RelationshipLaravel MailLaravel MiddlewareLaravel ObserverLaravel PackagesLaravel PaginationLaravel Permissions for storageLaravel PoliciesLaravel QueuesLaravel RequestsLaravel Route Model BindingLaravel RoutingLaravel SeedingLaravel ServicesLaravel SocialiteLaravel Task SchedulingLaravel TestingLaravel Token Mismatch Error in AJAXLaravel use fields aliases in EloquentLaravel Useful linksLaravel ValetLaravel ValidationMultiple DB Connections in LaravelNaming Files when uploading with Laravel on WindowsRemove public from URL in laravelSparkpost integration with Laravel 5.4



Laravel Pagination

From WikiOD

Pagination in Laravel[edit | edit source]

In other frameworks pagination is headache. Laravel makes it breeze, it can generate pagination by adding few lines of code in Controller and View.

Basic Usage

There are many ways to paginate items, but the simplest one is using the paginate method on query builder or an Eloquent query. Laravel out of the box take care of setting limit and offset based on the current page being viewed by user. By default, the current page is detected by the value of ?page query string argument on the HTTP request. And for sure, this value is detected by Laravel automatically and insert into links generated by paginator.

Now let's say we want to call the paginate method on query. In our example the passed argument to paginate is the number of items you would like to display "per page". In our case, let say we want to display 10 items per page.

<?php

namespace App\Http\Controllers;

use DB;
use App\Http\Controllers\Controller;

class UserController extends Controller
{
    /**
     * Show all of the users for the application.
     *
     * @return Response
     */
    public function index()
    {
        $users = DB::table('users')->paginate(10);

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

Note: Currently, pagination operations that use a groupBy statement cannot be executed efficiently by Laravel. If you need to use a groupBy with a paginated result set, it is recommended that you query the database and create a paginator manually.

Simple Pagination

Let say you just want to display Next and Previous links on your pagination view. Laravel provides you this option by using simplePaginate method.

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

Displaying Results In A View

Now lets display the pagination in view. Actually when you call the paginate or simplePaginate methods on Eloquent query, you receive a paginator instance. When paginate method is called, you receive an instance of Illuminate\Pagination\LengthAwarePaginator, while when you call simplePaginate method, you receive an instance of Illuminate\Pagination\Paginator. These instances / objects comes with several methods that explaines the result set. Moreover, in addition to these helpers methods, the paginator instances are iterators and can be looped as an array.

Once you received the results, you can easily render the page links using blade

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

{{ $users->links() }}

The links method will automatically render the links to other pages in result set. Each of these links will contain the specific page number i.e ?page query string variable. The HTML generated by the links method is perfectly compatible with the Bootstrap CSS framework.

Changing pagination views[edit | edit source]

While using laravel pagination you are free to use your own custom views.So,when calling the links method on a paginator instance, pass the view name as the first argument to the method like :

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

or

You can 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

This command will place the views in the resources/views/vendor/pagination directory. The default.blade.php file within this directory corresponds to the default pagination view. Edit this file to modify the HTML of pagination.

Credit:Stack_Overflow_Documentation