Fast Paginate for Laravel 9

Published on : July 27,2022
Fast Paginate for Laravel 9

Hi Developer,

In this article will learn about Fast Paginate with Laravel 9

This is a fast limit/offset pagination macro for Laravel. It can be used in place of the standard paginate methods.

This packages uses a SQL method similar to a "deferred join" to achieve this speedup. A deferred join is a technique that defers access to requested columns until after the offset and limit have been applied.

In our case we don't actually do a join, but rather a where in with a subquery. Using this technique we create a subquery that can be optimized with specific indexes for maximum speed and then use those results to fetch the full rows.

The SQL looks something like this:

select * from contacts              -- The full data that you want to show your users.
    where contacts.id in (          -- The "deferred join" or subquery, in our case.
        select id from contacts     -- The pagination, accessing as little data as possible - ID only.
        limit 15 offset 150000      
    )

You might get an error trying to run the query above! Something like This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery. In this package, we run them as two separate queries to get around that!

The benefits can vary based on your dataset, but this method allows the database to examine as little data as possible satisfy the user's intent.

It's unlikely that this method will ever perform worse than traditional offset / limit, although it is possible, so be sure to test on your data!

 $paginator = $this->clone()
                // Only select the primary key, we'll get the full
                // records in a second query below.
                ->select($innerSelectColumns)
                // We don't need eager loads for this cloned query, they'll
                // remain on the query that actually gets the records.
                // (withoutEagerLoads not available on Laravel 8.)
                ->setEagerLoads([])
                ->paginate($perPage, ['*'], $pageName, $page);

            // Get the key values from the records on the current page without mutating them.
            $ids = $paginator->getCollection()->map->getRawOriginal($key)->toArray();

            if ($model->getKeyType() === 'int') {
                $this->query->whereIntegerInRaw("$table.$key", $ids);
            } else {
                $this->query->whereIn("$table.$key", $ids);
            }

 

Installation

This package supports Laravel 8 and 9. (Laravel 8 must be 8.37 or higher.)

To install, require the package via composer:

composer require hammerstone/fast-paginate

There is nothing further you need to do. The service provider will be loaded automatically by Laravel.

 

Usage

Anywhere you would use Model::query()->paginate(), you can use Model::query()->fastPaginate()! That's it! The method signature is the same.

Relationships are supported as well:

User::first()->posts()->fastPaginate();

 

Hope it can help you…

Categories : Laravel PHP

Tags : PHP Laravel

Praful Sangani
Praful Sangani
I'm a passionate full-stack developer with expertise in PHP, Laravel, Angular, React Js, Vue, Node, Javascript, JQuery, Codeigniter, and Bootstrap. I enjoy sharing my knowledge by writing tutorials and providing tips to others in the industry. I prioritize consistency and hard work, and I always aim to improve my skills to keep up with the latest advancements. As the owner of Open Code Solution, I'm committed to providing high-quality services to help clients achieve their business goals.


27 Comments

fenofibrate 160mg drug where to buy fenofibrate without a prescription tricor oral


buy cialis tablets sildenafil 100mg usa order sildenafil online cheap


zaditor order online buy generic doxepin 25mg purchase imipramine generic


minoxidil generic tadalafil for women medication for ed


aspirin usa imiquimod oral where to buy zovirax without a prescription


melatonin 3 mg price cost norethindrone 5 mg purchase danazol online cheap


dipyridamole 100mg pills buy felodipine 5mg online buy cheap pravachol


buy duphaston 10 mg online cheap empagliflozin 25mg jardiance over the counter


fludrocortisone 100mcg over the counter rabeprazole 20mg tablet cheap imodium


buy mestinon 60 mg generic rizatriptan 10mg drug buy maxalt 10mg generic


buy ferrous 100 mg without prescription sotalol uk order sotalol 40 mg pills


vasotec pills order vasotec 5mg without prescription duphalac canada


purchase zovirax for sale exelon ca rivastigmine 3mg canada


cost betahistine order haloperidol 10mg online cheap probenecid 500mg for sale


premarin 600 mg without prescription premarin 0.625mg oral viagra pills


generic omeprazole 10mg montelukast oral metoprolol brand


tadalafil without prescription order generic tadalafil 5mg purchase viagra generic


order micardis 80mg generic buy molnupiravir 200 mg without prescription cheap molnunat 200mg


order cenforce 100mg generic chloroquine order online chloroquine online


modafinil 100mg cost order deltasone 20mg sale order prednisone 5mg for sale


buy omnicef glucophage 500mg for sale purchase lansoprazole for sale


cost accutane absorica online buy azithromycin 250mg without prescription


buy generic azithromycin over the counter oral gabapentin 100mg generic neurontin 100mg


atorvastatin pill order norvasc 5mg for sale norvasc 10mg sale


online casino games best online casino usa buy lasix 100mg sale


purchase protonix online cheap protonix ca phenazopyridine online


free spins no deposit uk gambling site buy ventolin for sale


Leave a comment

We'll never share your email with anyone else. Required fields are marked *

Related Articles

Access specifier in php
Praful Sangani By Praful Sangani - July 20,2022