All Projects → lampager → lampager-laravel

lampager / lampager-laravel

Licence: MIT license
Rapid pagination for Laravel

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to lampager-laravel

reactionmenu
A library to create a discord paginator. Supports pagination with Discords Buttons feature and reactions.
Stars: ✭ 68 (-4.23%)
Mutual labels:  pagination, paginator
React Paginate
A ReactJS component that creates a pagination
Stars: ✭ 2,169 (+2954.93%)
Mutual labels:  pagination, paginator
mongoose-aggregate-paginate-v2
A cursor based custom aggregate pagination library for Mongoose with customizable labels.
Stars: ✭ 103 (+45.07%)
Mutual labels:  pagination, paginator
te4j
Fast and easy template engine
Stars: ✭ 20 (-71.83%)
Mutual labels:  fast
next
(Work in progress) The rewritten version of the original PizzaQL 🍕
Stars: ✭ 45 (-36.62%)
Mutual labels:  fast
discord-paginationembed
A pagination utility for MessageEmbed in Discord.JS
Stars: ✭ 93 (+30.99%)
Mutual labels:  pagination
osprey-delight
Osprey Delight is the free-minded artist's choice for a clutter-free and blazingly fast single-page portfolio.
Stars: ✭ 43 (-39.44%)
Mutual labels:  fast
brute-md5
Advanced, Light Weight & Extremely Fast MD5 Cracker/Decoder/Decryptor written in Python 3
Stars: ✭ 16 (-77.46%)
Mutual labels:  fast
cortexm-AES
high performance AES implementations optimized for cortex-m microcontrollers
Stars: ✭ 18 (-74.65%)
Mutual labels:  fast
graphql-compose-pagination
Plugin for TypeComposer (graphql-compose), that adds `pagination` resolver.
Stars: ✭ 29 (-59.15%)
Mutual labels:  pagination
node-perj
A fast, flexible JSON logger.
Stars: ✭ 16 (-77.46%)
Mutual labels:  fast
spring-boot-jpa-rest-demo-filter-paging-sorting
Spring Boot Data JPA with Filter, Pagination and Sorting
Stars: ✭ 70 (-1.41%)
Mutual labels:  pagination
duckphp
PHP框架,PHP Framework. keep PHP simple and fast. Laravel larva and Smarty is evil
Stars: ✭ 125 (+76.06%)
Mutual labels:  fast
RxPagination
Implement pagination in just few lines with RxPagination
Stars: ✭ 20 (-71.83%)
Mutual labels:  pagination
HArray
Fastest Trie structure (Linux & Windows)
Stars: ✭ 89 (+25.35%)
Mutual labels:  fast
FAST
A GML supplement library for GMS 2.3+
Stars: ✭ 22 (-69.01%)
Mutual labels:  fast
jekyll-pagination
Better pagination for Jekyll.
Stars: ✭ 19 (-73.24%)
Mutual labels:  pagination
SwiftyUIScrollView
A custom ScrollView wrapper that comes with Pagination and Page Control.
Stars: ✭ 18 (-74.65%)
Mutual labels:  pagination
ultra-sort
DSL for SIMD Sorting on AVX2 & AVX512
Stars: ✭ 29 (-59.15%)
Mutual labels:  fast
cookie
Landing website + Blog using Jekyll & Tailwind CSS
Stars: ✭ 61 (-14.08%)
Mutual labels:  fast

lampager-laravel

Build Status Coverage Status Scrutinizer Code Quality

Lampager for Laravel

Rapid pagination without using OFFSET

Requirements

  • PHP: ^7.3 || ^8.0
  • Laravel: ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0
  • lampager/lampager: ^0.4

Installing

composer require lampager/lampager-laravel

Basic Usage

Register service provider.

config/app.php:

        /*
         * Package Service Providers...
         */
        Lampager\Laravel\MacroServiceProvider::class,

Then you can chain ->lampager() method from Query Builder, Eloquent Builder and Relation.

$cursor = [
    'id' => 3,
    'created_at' => '2017-01-10 00:00:00',
    'updated_at' => '2017-01-20 00:00:00',
];

$result = App\Post::whereUserId(1)
    ->lampager()
    ->forward()
    ->limit(5)
    ->orderByDesc('updated_at') // ORDER BY `updated_at` DESC, `created_at` DESC, `id` DESC
    ->orderByDesc('created_at')
    ->orderByDesc('id')
    ->seekable()
    ->paginate($cursor)
    ->toJson(JSON_PRETTY_PRINT);

It will run the optimized query.

(

    SELECT * FROM `posts`
    WHERE `user_id` = 1
    AND (
        `updated_at` = '2017-01-20 00:00:00' AND `created_at` = '2017-01-10 00:00:00' AND `id` > 3
        OR
        `updated_at` = '2017-01-20 00:00:00' AND `created_at` > '2017-01-10 00:00:00'
        OR
        `updated_at` > '2017-01-20 00:00:00'
    )
    ORDER BY `updated_at` ASC, `created_at` ASC, `id` ASC
    LIMIT 1

) UNION ALL (

    SELECT * FROM `posts`
    WHERE `user_id` = 1
    AND (
        `updated_at` = '2017-01-20 00:00:00' AND `created_at` = '2017-01-10 00:00:00' AND `id` <= 3
        OR
        `updated_at` = '2017-01-20 00:00:00' AND `created_at` < '2017-01-10 00:00:00'
        OR
        `updated_at` < '2017-01-20 00:00:00'
    )
    ORDER BY `updated_at` DESC, `created_at` DESC, `id` DESC
    LIMIT 6

)

And you'll get

{
  "records": [
    {
      "id": 3,
      "user_id": 1,
      "text": "foo",
      "created_at": "2017-01-10 00:00:00",
      "updated_at": "2017-01-20 00:00:00"
    },
    {
      "id": 5,
      "user_id": 1,
      "text": "bar",
      "created_at": "2017-01-05 00:00:00",
      "updated_at": "2017-01-20 00:00:00"
    },
    {
      "id": 4,
      "user_id": 1,
      "text": "baz",
      "created_at": "2017-01-05 00:00:00",
      "updated_at": "2017-01-20 00:00:00"
    },
    {
      "id": 2,
      "user_id": 1,
      "text": "qux",
      "created_at": "2017-01-17 00:00:00",
      "updated_at": "2017-01-18 00:00:00"
    },
    {
      "id": 1,
      "user_id": 1,
      "text": "quux",
      "created_at": "2017-01-16 00:00:00",
      "updated_at": "2017-01-18 00:00:00"
    }
  ],
  "has_previous": false,
  "previous_cursor": null,
  "has_next": true,
  "next_cursor": {
    "updated_at": "2017-01-18 00:00:00",
    "created_at": "2017-01-14 00:00:00",
    "id": 6
  }
}

Resource Collection

Lampager supports Laravel's API Resources.

Use helper traits on Resource and ResourceCollection.

use Illuminate\Http\Resources\Json\JsonResource;
use Lampager\Laravel\LampagerResourceTrait;

class PostResource extends JsonResource
{
    use LampagerResourceTrait;
}
use Illuminate\Http\Resources\Json\ResourceCollection;
use Lampager\Laravel\LampagerResourceCollectionTrait;

class PostResourceCollection extends ResourceCollection
{
    use LampagerResourceCollectionTrait;
}
$posts = App\Post::lampager()
    ->orderByDesc('id')
    ->paginate();

return new PostResourceCollection($posts);
{
  "data": [/* ... */],
  "has_previous": false,
  "previous_cursor": null,
  "has_next": true,
  "next_cursor": {/* ... */}
}

Classes

Note: See also lampager/lampager.

Name Type Parent Class Description
Lampager\Laravel\Paginator Class Lampager\Paginator Fluent factory implementation for Laravel
Lampager\Laravel\Processor Class Lampager\AbstractProcessor Processor implementation for Laravel
Lampager\Laravel\PaginationResult Class Lampager\PaginationResult PaginationResult implementation for Laravel
Lampager\Laravel\MacroServiceProvider Class Illuminate\Support\ServiceProvider Enable macros chainable from QueryBuilder, ElqouentBuilder and Relation
Lampager\Laravel\LampagerResourceTrait Trait Support for Laravel JsonResource
Lampager\Laravel\LampagerResourceCollectionTrait Trait Support for Laravel ResourceCollection

Paginator, Processor and PaginationResult are macroable.

API

Note: See also lampager/lampager.

Paginator::__construct()
Paginator::create()

Create a new paginator instance.
If you use Laravel macros, however, you don't need to directly instantiate.

static Paginator create(QueryBuilder|EloquentBuilder|Relation $builder): static
Paginator::__construct(QueryBuilder|EloquentBuilder|Relation $builder)
  • QueryBuilder means \Illuminate\Database\Query\Builder
  • EloquentBuilder means \Illuminate\Database\Eloquent\Builder
  • Relation means \Illuminate\Database\Eloquent\Relation

Paginator::transform()

Transform Lampager Query into Illuminate builder.

Paginator::transform(Query $query): QueryBuilder|EloquentBuilder|Relation

Paginator::build()

Perform configure + transform.

Paginator::build(\Lampager\Contracts\Cursor|array $cursor = []): QueryBuilder|EloquentBuilder|Relation

Paginator::paginate()

Perform configure + transform + process.

Paginator::paginate(\Lampager\Contracts\Cursor|array $cursor = []): \Lampager\Laravel\PaginationResult

Arguments

  • (mixed) $cursor
    An associative array that contains $column => $value or an object that implements \Lampager\Contracts\Cursor. It must be all-or-nothing.
    • For initial page, omit this parameter or pass empty array.
    • For subsequent pages, pass all parameters. Partial parameters are not allowd.

Return Value

e.g.

(Default format when using \Illuminate\Database\Eloquent\Builder)

object(Lampager\Laravel\PaginationResult)#1 (5) {
  ["records"]=>
  object(Illuminate\Database\Eloquent\Collection)#2 (1) {
    ["items":protected]=>
    array(5) {
      [0]=>
      object(App\Post)#2 (26) { ... }
      [1]=>
      object(App\Post)#3 (26) { ... }
      [2]=>
      object(App\Post)#4 (26) { ... }
      [3]=>
      object(App\Post)#5 (26) { ... }
      [4]=>
      object(App\Post)#6 (26) { ... }
    }
  }
  ["hasPrevious"]=>
  bool(false)
  ["previousCursor"]=>
  NULL
  ["hasNext"]=>
  bool(true)
  ["nextCursor"]=>
  array(2) {
    ["updated_at"]=>
    string(19) "2017-01-18 00:00:00"
    ["created_at"]=>
    string(19) "2017-01-14 00:00:00"
    ["id"]=>
    int(6)
  }
}

Paginator::useFormatter()
Paginator::restoreFormatter()
Paginator::process()

Invoke Processor methods.

Paginator::useFormatter(Formatter|callable $formatter): $this
Paginator::restoreFormatter(): $this
Paginator::process(\Lampager\Query $query, \Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Collection $rows): \Lampager\Laravel\PaginationResult

PaginationResult::toArray()
PaginationResult::jsonSerialize()

Convert the object into array.

IMPORTANT: camelCase properties are converted into snake_case form.

PaginationResult::toArray(): array
PaginationResult::jsonSerialize(): array

PaginationResult::__call()

Call macro or Collection methods.

PaginationResult::__call(string $name, array $args): mixed

e.g.

PaginationResult::macro('foo', function () {
    return ...;
});
$foo = $result->foo();
$first = $result->first();
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].