Git Product home page Git Product logo

laravel-paginator's Introduction

Laravel Paginator

Make the pagination for arrays or Collections

Installation

Composer require

$ composer require amamarul/laravel-paginator

Add Provider into config/app.php

Amamarul\Paginator\PaginatorServiceProvider::class,

Usage

In Controller

  • Array
use Amamarul\Paginator\Paginator;
use Illuminate\Http\Request;

public function index(Request $request)
{
    $currentPage  = isset($request['page']) ? (int) $request['page'] : 1;
    $perPage      = 1;
    $path         = $request->path();

    $items = array_map(function ($value) {
        return [
        'name' => 'User #' . $value,
        'url'  => '/user/' . $value,
        ];
        }, range(1,1000));

        $paginator = new Paginator($items);
        $paginator = $paginator->paginate($currentPage,$perPage, $path);

    return view('index')->with('paginator', $paginator);
}
  • Collection
use App\User;
use Amamarul\Paginator\Paginator;
use Illuminate\Http\Request;

public function index(Request $request)
{
    $currentPage  = isset($request['page']) ? (int) $request['page'] : 1;
    $perPage      = 1;
    $path         = $request->path();

    $items = User::with('profile')->get()->sortBy('profile.name');

    $paginator = new Paginator($items);
    $paginator = $paginator->paginate($currentPage,$perPage, $path);

    return view('index')->with('paginator', $paginator);
}

In Blade View (index.blade.php)

@foreach ($paginator->items() as $element)
    <a href="{!!$element['url']!!}"><h3>{!!$element['name']!!}</h3></a>
@endforeach

{!! $paginator->render() !!}

Customize Page Name

By default the url has page name http://127.0.0.1:8000/?page=3 If you´d like to change the page name yo must only add a fourth parameter with the name. Like this

use App\User;
use Amamarul\Paginator\Paginator;
use Illuminate\Http\Request;

public function index(Request $request)
{
    $currentPage  = isset($request[$pageName]) ? (int) $request[$pageName] : 1;
    $perPage      = 1;
    $path         = $request->path();
    $pageName     = 'custom-name';

    $items = User::with('profile')->get()->sortBy('profile.name');

    $paginator = new Paginator($items);
    $paginator = $paginator->paginate($currentPage,$perPage, $path, $pageName);

    return view('index')->with('paginator', $paginator);
}

Feel free to send improvements

Created by Maru Amallo-amamarul

laravel-paginator's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.