Pagination

Funny Laravel docs · Warning: May cause actual learning AND laughter!

Browse funny laravel docs
On this page

Database: The Magical Scroll of Infinite Data! ๐Ÿง™โ€โ™‚๏ธ๐Ÿ“–

Chapter 1 - Introduction to the Scroll of Infinite Data ๐Ÿ”

Welcome, brave data seeker! Youโ€™ve stumbled upon Laravelโ€™s mightiest tool: The Magical Scroll of Infinite Data. This scroll will help you navigate through colossal amounts of data without getting lost in the labyrinth of endless records. Buckle up, letโ€™s dive in!

Chapter 2 - Basic Usage of the Magical Scroll ๐Ÿงฉ

Section A - Paginating Query Builder Results (The Wand of Many Queries) ๐ŸŽญ

Cast this spell to split your data into manageable chunks and watch them parade by like a spectacle at a medieval fair.

$data = DB::table('users')
    ->orderBy('id', 'asc')
    ->paginate(15);

Section B - Paginating Eloquent Results (The Scepter of Single Queries) ๐Ÿ‘‘

Eloquent models can also be paginated! Use this to show data in a more regal manner, fitting for your kingdomโ€™s subjects.

$users = User::orderBy('id', 'asc')
    ->paginate(15);

Section C - Cursor Pagination (The Crystal Ball of Continuous Scrolling) ๐Ÿ”ฎ

For those who want an endless scroll, this spell is for you! Watch your data glide by as if time itself has no end.

use Illuminate\Support\Facades\DB;

$cursor = DB::table('users')->simplePaginate(0, ['*'], 'id', $nextCursor);

if ($cursor->hasMorePages()) {
    // Scroll down!
}

Section D - Manually Creating a Paginator (The Book of Custom Spells) ๐Ÿ“–

Sometimes, the default scroll is not enough. When you need something more unique, craft your own custom paginator with this spell!

use Illuminate\Pagination\LengthAwarePaginator;

$results = User::all();

$paginatedResults = new LengthAwarePaginator($results, $results->count(), 15);

Section E - Customizing Pagination URLs (The Wand of SEO Magic) ๐ŸŒ

Make sure your magical scroll doesnโ€™t leave a trail of chaos in its wake. Customize your pagination links to keep search engines and users happy!

Chapter 3 - Displaying Paginated Results (The Scroll of Userly Delights) ๐Ÿ“ž

Sometimes, less is more. Focus your usersโ€™ attention on specific pages in the paginated scroll.

$data = DB::table('users')
    ->orderBy('id', 'asc')
    ->simplePaginate(15, ['*'], 'page', 2, 3);

Section B - Converting Results to JSON (The Charm of Data Transformation) ๐Ÿ”„

Transform your paginated scroll into a more compact form for easier data manipulation!

$users = User::orderBy('id', 'asc')
    ->simplePaginate(15);

echo json_encode($users);

Chapter 4 - Customizing the Pagination View (The Spell of Elegant Design) ๐ŸŽจ

Dress up your magical scroll with a custom design, fit for even the pickiest of data enthusiasts. Why settle for a plain old scroll when you can have one that sparkles and shimmers?

use Illuminate\Pagination\Paginator;

class CustomPaginator extends Paginator {
    // Add your customizations here!
}

Chapter 5 - Paginator and LengthAwarePaginator Instance Methods (The Scrollโ€™s Secret Powers) ๐Ÿ”’

Unlock the hidden powers within your magical scroll, from getting more information about the current page to jumping ahead in the scroll with ease!

Chapter 6 - Cursor Paginator Instance Methods (The Scrollโ€™s Continuous Magic) ๐ŸŒ™

Harness the power of continuous scrolling, where the end is never in sight! With cursor pagination, you can keep users hooked on your data like a never-ending Netflix series.

Pagination: A Breeze in Laravelville! ๐Ÿš€

In other frameworks, pagination might as well be a root canal - painful and long-lasting. But fear not, dear developer! In Laravel, weโ€™ve got a fresh breeze of relief for your query needs! Our magical paginator, integrated with the Query Builder and the all-powerful Eloquent ORM, will make database record pagination as easy as pieโ€ฆ or perhaps pizza, because who doesnโ€™t love pizza?

Best part? Zero configuration required! Yes, you heard that right. Zip, zero, zilch! Just ask and you shall receive.

By default, our paginator generates HTML that harmonizes beautifully with the Tailwind CSS framework. But if Tailwindโ€™s not your thing (weโ€™re not sure how thatโ€™s possible, but each to their own), weโ€™ve got Bootstrap support ready and waiting!

Now, wasnโ€™t that a pleasant introduction? Letโ€™s dive in, shall we? ๐Ÿ  (Yes, that was a Finding Nemo reference. We couldnโ€™t help ourselves.)

Alrighty, partner! If youโ€™re rockinโ€™ Laravelโ€™s Tailwind Express Train with Tailwind 4.x, your appโ€™s resources/css/app.css file is already decked out in style like a Las Vegas lounge on Saturday night!

Here's the rundown:

@import 'tailwindcss';

And now, for the main event...
@source '../../vendor/laravel/framework/src/Illuminate/Pagination/resources/views/*.blade.php';

Now you can strut your stuff like a digital cowboy with Laravelโ€™s pagination views in perfect sync with Tailwind! Just remember, no two-steppinโ€™ or line-dancinโ€™ allowed in the CSS files! ๐Ÿค ๐ŸŽ‰

Ahoy there, Captain! Letโ€™s hoist the sails of knowledge and set course for Laravelโ€™s QueryBuilder Pagination Islands!


First off, you might find yourself drowning in a sea of results โ€“ a veritable tsunami of data, if you will. Fear not, dear sailor, for Laravel offers a trusty olโ€™ lifeboat called pagination to help you navigate this stormy ocean!


To hitch a ride on this lifeboat, simply append the paginate() method to your QueryBuilder query:

$data = App\User::orderBy('created_at', 'desc')->paginate(15);

Here, weโ€™re ordering our users by their creation date (because who wants stale users?) and limiting the number of passengers aboard this ship to a manageable 15.


Now, as you sail through the data, Laravel will kindly hand you pages of results (in the form of the \Illuminate\Pagination\LengthAwarePaginator class), complete with navigation links and all!


You can even customize the navigation links by messing around with the onEachSide(), total(), and pageName methods:

$data = App\User::orderBy('created_at', 'desc')
    ->paginate(15, ['*'], 'customPageName', 2);

In this example, weโ€™re using the customPageName parameter to rename our navigation links from โ€œPage Xโ€ to something a bit more pirate-y, like โ€œShanty 2.โ€ And remember, mateys: You can adjust the number of links displayed on either side of the current page using the onEachSide() method (default is one).


Lastly, if you want to keep a count of how many sharks your query has bitten off (er, results it has fetched), you can use the total() method:

$totalUsers = App\User::count();

And there ya have it, me hearties! Laravelโ€™s QueryBuilder pagination is as handy as a shipโ€™s wheel in stormy weather. Now go forth and conquer the seas of data! Yarr! ๐Ÿดโ€โ˜ ๏ธโš“๏ธ

Paginating Query Builder Results (AKA โ€œTurning One Big Party into a Never-Ending Sequelโ€)

Welcome to the wild world of pagination, where we transform a single data dump into a series of manageable events (or pages), each waiting eagerly for their time in the spotlight. And guess what? Laravelโ€™s got your back with some pretty nifty tools!

In this jamboree, there are several ways to paginate items, but letโ€™s start with the simplest one: by using the paginate method on the query builder or an Eloquent query. The paginate method is like a magical bouncer that takes care of setting the queryโ€™s โ€œlimitโ€ and โ€œoffsetโ€ based on the current page being viewed by your users, all without lifting a finger!

By default, the current page is detected by the value of the page query string argument on the HTTP request. This value is automatically detected by Laravel, and itโ€™s like magic โ€“ just add it to your URL, and voila! Your users can navigate seamlessly through your data pages. The icing on the cake? This value is also automatically inserted into links generated by the paginator, making navigation a breeze for both you and your visitors.

Hereโ€™s an example of how you can use paginate to serve up a delicious helping of users per page:

<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\DB;
use Illuminate\View\View;

class UserController extends Controller
{
    /**
     * Show all application users.
     */
    public function index(): View
    {
        // We're serving up a scrumptious 15 users per page!
        $users = DB::table('users')->paginate(15);

        // Now, let's serve these users with a view that loves them!
        return view('user.index', compact('users'));
    }
}

Now, wasnโ€™t that a fun-filled lesson? Remember to keep the party going and explore more ways of paginating in Laravel!

Ahoy there, coding pirates! Letโ€™s dive into the tantalizing topic of Simple Pagination, shall we?

First off, let me tell you about the trusty paginate method. This chap not only fetches records from the database but also counts โ€˜em like a pro beforehand. Why you ask? Well, itโ€™s like him saying โ€œArrr, mateys! There be 5 treasure chests full of gold!โ€ before leading you to them so you know how many journeys youโ€™ll have to make. But if ye donโ€™t fancy showing the exact number oโ€™ chests in yer appโ€™s UI, well then ye can just skip that part, matey!

Now, if all ye need is a few simple โ€œNext!โ€ and โ€œPrevious!โ€ links to guide your users, the simplePaginate method be the one for ye. This lad performs a swift, efficient query that gets the job done without any unnecessary record counting piracy:

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

In this example, weโ€™re summoning 15 hearty souls (errโ€ฆ users) to their digital ship, and off they go on a simple yet efficient voyage through the sea of data! Ah, isnโ€™t coding fun? Letโ€™s keep sailing together! \o/

Alrighty, buckle up, coders! Letโ€™s dive into the world of paginating Eloquent queries. Itโ€™s like organizing a rambunctious house party where you want to ensure no one gets overwhelmed - just replace โ€˜house partyโ€™ with โ€˜database resultsโ€™, and โ€˜guestsโ€™ with โ€˜recordsโ€™.

First things first, letโ€™s paginate our beloved App\Models\User model. Weโ€™re planning a swanky soiree of 15 records per page (because who needs more friends, amirite?). Check out this slick code snippet:

use App\Models\User;

$users = User::paginate(15);

Now, if youโ€™re feeling fancy and want to add some filters, like a โ€˜votesโ€™ clause (because who doesnโ€™t love a good debate?), just go ahead:

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

Fancy pants, arenโ€™t we? But wait, thereโ€™s more! You can also use the simplePaginate method when paginating Eloquent models (because sometimes you just want to keep it simple, stupid):

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

And if youโ€™re feeling extra adventurous, you can try out cursorPaginate (because who doesnโ€™t love a good cursor dance party?):

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

Remember, itโ€™s always a good idea to serve your records with style and grace! Now, go forth and paginate like a boss! ๐ŸŽ‰๐Ÿฅณ

Oh, the saga of two paginators on a date! One night, they both wanted to steal the spotlight on your application screen. But alas, their simultaneous craving for the page query string param caused chaos, like a dance floor brawl at a disco. Fret not, dear developer! Laravelโ€™s got your back with a cunning solution thatโ€™s as slick as a panther in moonlight.

Just pass the name of the desired query string parameter to the paginatorโ€™s current page, like you would invite the wallflower to join the dance:

use App\Models\User;

$users = User::where('votes', '>', 100)->paginate(
    $perPage = 15, $columns = ['*'], $pageName = 'users'
);

In this charming encounter, users is the wallflower, who will now be known by its unique name instead of the default page. So, no more confusion over which one to twirl with! ๐Ÿ•บ๐Ÿš€โœจ

P.S. Curious about Cursor Pagination? Jump ahead to โ€œCursor Paginationโ€ (anchor link), or simply follow the dance floor lights to find it!

Cursor Shuffle (aka Pagination 2.0)

If youโ€™ve ever felt like your database was a rowdy party and you just wanted to politely ask for the next tune, then youโ€™re going to love our new Cursor Shuffle feature! Unlike traditional offset-based pagination that acts like an awkward guest who barges into the middle of the party (and sometimes skips some tunes), Cursor Shuffle elegantly slides in right where the music is already grooving.

Imagine youโ€™re a DJ, and instead of shouting โ€œHey, come over here, itโ€™s tune number 27!โ€ to your guests, you subtly drop hints like โ€œNext up, track with the shimmering sequins.โ€ Thatโ€™s what Cursor Shuffle does for our Laravel party!

Unlike its offset cousin, which screams page numbers in the URLs it generates (guess whoโ€™s the over-eager newcomer at the bash?), Cursor Shuffle discreetly drops a โ€œcursorโ€ string into the query. The cursor is like a coded invitation with the location of the next dance and the direction we should boogie:

http://localhost/users?cursor=eyJpZCI6MTUsIl9wb2ludHNUb05leHRJdGVtcyI6dHJ1ZX0

You can create a Cursor Shuffle playlist by using the cursorPaginate method, which is like asking your favorite DJ to spin the tunes for you. Just give it a call with your query builder:

$users = DB::table('users')->orderBy('id')->cursorPaginate(15);

Once youโ€™ve got your Cursor Shuffle playlist, feel free to spin the tunes as you would with paginate and simplePaginate. For more deets on the dance moves offered by our new friend, check out the Cursor Shuffle instance method documentation.

โš ๏ธ WARNING! To make use of this groovy feature, you gotta have your query sorted like a well-organized playlist. Plus, the tracks on that list should belong to the same album (aka table) youโ€™re partying in.

Now spinning tunes like a boss! ๐ŸŽถ ๐Ÿ•บ๏ธ

Alright, buckle up, folks! Letโ€™s dive into the world of pagination and see if we can make sense of two unlikely heroes: Offset Pagination and Cursor Pagination.

Imagine youโ€™re at a never-ending party where everyone is dancing, chatting, and occasionally taking selfies (in a Laravel users table). Now, there are two ways to approach serving these selfies - the chaotic Offset way, and the cool Cursor way.

Letโ€™s start with our old pal, Offset Pagination:

SELECT * FROM users OFFSET 10 LIMIT 10;

This query is like telling the DJ to skip the first 10 selfies (the โ€œfirst pageโ€) and play the next 10. The problem? If someone sneaks in a selfie between our party-goers, we miss it! Offset pagination doesnโ€™t care about the newcomer - it just moves on with its life.

Now, letโ€™s meet our new pal, Cursor Pagination:

SELECT * FROM users WHERE id > (SELECT id FROM users ORDER BY id LIMIT 10 OFFSET 10);

This query is like asking the DJ for โ€œthe next selfie after the tenth one we skipped.โ€ It keeps track of the last selfie it played and doesnโ€™t miss any newcomers! If a selfie-taking partygoer shows up between our previous set, Cursor Pagination will grab that selfie without breaking a sweat.

So, there you have it! Offset Pagination is great for simple parties where nobody joins or leaves after the host says โ€œcheese.โ€ But if your party is more like a bustling dance floor, go with Cursor Pagination - it keeps things fresh and ensures no selfies are left behind.

Ahoy, Captain! Navigate through the vast sea of data like a seasoned seafarer with Laravelโ€™sOFFSET PAGINATION - the compass for your next cod-quest!

(Imagine youโ€™re on a pirate ship, sailing through endless seas of users data)

โ€œYo ho, matey! Sail no more to the perilous โ€˜All Usersโ€™ page! With OFFSET PAGINATION by our side, we shall only fetch 15 new users at a time. So, anchor those swords, and grab your trusty SQL query!โ€

(Now itโ€™s time to write the magical incantation)

โ€œSelect every user from the land of โ€˜users,โ€™ order them alphabetically by their ID, ascending, set the limit to 15, then take us forward 15 steps with the OFFSET magic. Thus, we shanโ€™t have to deal with the whole sea of users at once!โ€

(In SQL terms)

select * from users order by id asc limit 15 offset 15;

(Just a quick reminder!)

โ€œSo, there ye have it! With OFFSET PAGINATION, Laravel makes traversing the endless seas of data less daunting and more manageable - much like a well-stocked pirate ship!โ€

Alright, letโ€™s dive into the whimsical world of cursor pagination! Itโ€™s like inviting your nerdy friend over for a game night, except instead of Monopoly or D&D, weโ€™re playing Database Wizardry.

Imagine youโ€™re at a raging party, and everyone wants to know who the most senior attendee is. You could go through the whole guest list again and again (offset pagination), but thatโ€™s tedious, right? Enter cursor pagination - our efficient, well-dressed bouncer who only lets in the next 15 people on the list after checking IDs (indexed columns). He skips the drama of going through everyone again if you ask for the same information later (frequent writes).

But, our bouncer has a few quirks:

  • He can only show โ€œNextโ€ and โ€œPreviousโ€ signs (like simplePaginate). No fancy numbered programs or custom directions.
  • Heโ€™s quite picky about who he lets in - everyone must have a unique ID, no exceptions. And those with mysterious โ€˜nullโ€™ values are out of luck.
  • Heโ€™s also particular about his drinks, only accepting aliased query expressions (if theyโ€™re invited to the party too) and turning down any drinks with parameters.

Now that you know our cursor pagination bouncer, letโ€™s learn how to manually create a paginator for an unforgettable night! ๐Ÿค˜๐Ÿ’ƒ

Unleashing Your Inner Magician: Crafting a Pagination Sorcerer!

Ah, the thrill of manually creating a pagination spell! Imagine yourself as Merlin, conjuring up a bewitching potion from an array of enchanted items already residing in your memoryโ€™s mystical cauldron. To cast this spell, youโ€™ve got three powerful magic wands at your disposal: Illuminate\Pagination\Paginator, Illuminate\Pagination\LengthAwarePaginator, and Illuminate\Pagination\CursorPaginator. Choose wisely, my friend!

The Paginator and CursorPaginator wands are equipped with a cloak of invisibility - theyโ€™re oblivious to the total number of potions (items) in your magical result set. This means they canโ€™t help you find the mystical index of the final page, but hey, who needs that when youโ€™ve got a spell book full of other enchantments!

On the other hand, LengthAwarePaginator is the wise and well-informed member of our coven. It accepts almost all the same ingredients as the humble Paginator, but it also insists on knowing the grand total of potions in your result set - a crucial piece of information for those seeking the index of the last page.

In essence, the Paginator resembles the simplePaginate charm, the CursorPaginator corresponds to the cursorPaginate incantation, and the LengthAwarePaginator is like the versatile paginate spell.

Caution

When crafting your pagination sorcerer, remember to โ€œsliceโ€ the array of potions you pass to the cauldron. If youโ€™re struggling with this ancient PHP art, fear not! Consult the array_slice function - it will guide you through the process like a benevolent spirit.

Pagination URL Makeover: The Sequel to Cinderellaโ€™s Glass Slipper! ๐Ÿ‘ 

Well, hello there, coding mavericks! Ever found yourself in a pickle when your pagination links didnโ€™t quite match the Prince Charming of your applicationโ€™s URL structure? Fret not, because weโ€™ve got the glass slipper solution for you! ๐Ÿ‘ฝ

By design, our paginator plays matchmaker with the current URL. But, if you fancy a change and want to customize the URL used by the paginator while spinning your web of links, look no further than the paginatorโ€™s withPath method! Itโ€™s like introducing a secret handshake between your pagination and that perfect URL youโ€™ve been dreaming of.

For instance, if youโ€™re longing for links in the style of http://example.com/admin/users?page=N, all you need to do is whisper /admin/users into the ears of our humble withPath method:

use App\Models\User;

Route::get('/users', function () {
    $users = User::paginate(15); // Popcorn and butter ready? Let's dance! ๐Ÿฟ

    $users->withPath('/admin/users'); // Wink, wink! Told you it was a secret handshake.

    // ... And off to the ball we go!
});

Remember to appreciate the elegance and simplicity of this method, as itโ€™s just like a well-choreographed dance move that makes all the difference on the dance floor! ๐Ÿ’ƒ๐Ÿ•บ

Alrighty, letโ€™s get this pagination party started! You can spice up your linkโ€™s query strings like a master chef adds herbs to a dish. To do that, use the appends method, which is Laravelโ€™s version of a secret ingredient. Hereโ€™s how you can sprinkle some โ€œsort=votesโ€ magic on each pagination link:

use App\Models\User;

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

    // Time to season the links with 'sort=votes'
    $users->appends(['sort' => 'votes']);

    // ...
});

Now, if you want to go all-in and append every single query string value from your current request, well, my friend, youโ€™re going to love the withQueryString() method. Itโ€™s like hitting the โ€œadd all ingredientsโ€ button on a spice rack:

$users = User::paginate(15)->withQueryString();

And there you have it! Now, your pagination links will be seasoned to perfection, leaving no stone (or query string value) unturned. Happy traversing, dear coder! ๐Ÿด๐Ÿš€

Alrighty, buckle up, web explorers! If you find yourself yearning to slap a โ€˜hash fragmentโ€™ on those URLs spun by the Laravel paginator, fret not! Weโ€™ve got just the ticket for you โ€“ the enchanting fragment method!

Letโ€™s say youโ€™re hankering to tack on โ€œ#usersโ€ to the end of each link in your pagination party. Well, pull up a chair and get ready to code like a pro:

$peeps = User::paginate(15); // Ordinary pagination

// But now... magic happens!
$peeps->fragment('users'); // Sprinkles the hash fragment goodness on your links!

Remember, itโ€™s like adding the cherry on top of a delicious cake (or the โ€œhashโ€ in a hash-brownie, if you will) that makes everything just a tad more appetizing. So go forth and conquer those URLs with this delightful little helper! ๐ŸŽ‰๐ŸŒˆ๐Ÿš€

Alright, letโ€™s unravel the pagination mystery!

First off, when you summon the mighty paginate sorcerer, a knight in shining armor of Illuminate\Pagination\LengthAwarePaginator shall appear. Dial up simplePaginate, and behold a humbler Illuminate\Pagination\Paginator. Lastly, conjure cursorPaginate, and an advanced warrior named Illuminate\Pagination\CursorPaginator will take the stage!

These chivalrous companions come armed with helpful methods thatโ€™ll keep you informed about the battlefield, erโ€ฆ result set. After bagging your loot (results), you can showcase them in a medieval feast (HTML page) using Blade:

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

{{ $users->links() }}

The links method will forge links to the other realms in the result set. Each link comes already pre-armed with the page variable, so you can navigate between the lands with ease. And remember, the code generated by the links method is compatible with the Tailwind CSS framework.

Now, if you fancy customizing your link window, adjust the $pageName and $pathName variables in the service providerโ€™s boot method:

use Illuminate\Support\Facades\URL;
use Illuminate\Pagination\Paginator as BasePaginator;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        BasePaginator::defaultView('vendor.pagination.bootstrap-5');
        BasePaginator::renderOne(function (BasePaginator $paginator) {
            return $paginator->onEachSide(1)->render();
        });

        // Customize pagination URLs...
        if ($this->app->environment('local', 'testing')) {
            URL::defaults(['page' => 'results']);
        }
    }
}

By modifying the URL::defaults line, you can adjust the page query string variable. For instance, set it to โ€œsagaโ€ for a more dramatic feel:

URL::defaults(['page' => 'saga']);

Now, instead of saying page 2, you can shout โ€œChapter 2!โ€ as you navigate through your saga!

Taming the Pagination Beastie!

Ah, the thrill of navigating through a labyrinth of data pages! But have you ever wished that your dear paginator would offer a more personalized tour guide? Fear not, my friend! Laravel has come to the rescue with its onEachSide method, ready to be your Sherpa on this data journey.

Just like instructing Yoda on how many Dagobah swamps youโ€™d like to explore at once, tell your paginator how many extra links it should sprinkle on either side of the current page within its cozy sliding window of links:

{{ $users->onEachSide(5)->links() }}

And voilร ! Youโ€™ve just made your usersโ€™ data exploration a walk in the park, not an adventure through the Sarlacc pit. Now, get out there and conquer those data pages like a true Jedi! (Just remember to keep the Force strong, and donโ€™t feed the Sarlaccโ€ฆ)

JSONifying Paginated Results like a Boss in Laravel! ๐Ÿš€๐ŸŽง

Hey there, code slingers! ๐Ÿค˜ Letโ€™s dive into the world of sweet, sweet JSONification in Laravel. Yep, you read that right - weโ€™re making JSON as cool as Elon Muskโ€™s Tesla Roadster in space. ๐Ÿš€๐Ÿ›ฐ

The paginator classes in Laravel are not only suave but also implement the Illuminate\Contracts\Support\Jsonable Interface contract, giving you superpowers to convert your pagination results into JSON like a pro! ๐Ÿฆธโ€โ™‚๏ธ

To get started, letโ€™s pretend weโ€™re running a super-hip user management system. Hereโ€™s how you can create a simple route that returns a paginated list of users in the JSON format:

use App\Models\User;

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

Now, when someone accesses http://your-awesome-laravel-app.com/users, theyโ€™ll receive a JSON response that looks like this:

{
   "total": 50,
   "current_page": 1,
   "per_page": 15,
   "last_page": 4,
   "data": [
        {
            // User details...
        },
        {
            // User details...
        }
   ]
}

Youโ€™ll notice this JSON includes all sorts of useful meta information like the current page, total records, and more! The actual user data is conveniently stored under the data key in our JSON array. ๐Ÿ”‘

Now that you know how to paginate results and convert them into JSON, letโ€™s kick it up a notch with customizing the pagination view. But for now, letโ€™s celebrate this victory with a round of high-fives! ๐ŸŽ‰๐Ÿ’ช

Alright, letโ€™s dive into the world of Laravel pagination - where numbers meet design and comedy! By default, our pagination links are as sleek as a Tailwind CSS-wearing model strutting down the Fashion Week runway. But what if youโ€™re more of a denim-and-flannel kind of coder? No worries, we got you covered!

To create your own uniquepagination experience, simply tell Laravel to โ€œhold my beerโ€ by calling the links method on a paginator instance and passing your view name as the first argument:

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

<!-- And if you feel like sharing some extra secrets with your view... -->
{{ $paginator->links('view.name', ['foo' => 'bar']) }}

But hereโ€™s the real party trick - customizing pagination views is as easy as ordering a pizza at 3 AM! Export 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 your applicationโ€™s resources/views/vendor/pagination directory, just like a delivery guy dropping off your late-night feast. The tailwind.blade.php file within this directory is where you can modify the pagination HTML to make it your own masterpiece!

Now, if youโ€™re feeling particularly artistic and want to designate a different file as the default pagination view, all you need to do is invite the paginator to a little dance called boot. Invoke the paginatorโ€™s defaultView and defaultSimpleView methods within the boot method of your AppServiceProvider class:

<?php

namespace App\Providers;

use Illuminate\Pagination\Paginator;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     */
    public function boot(): void
    {
        Paginator::defaultView('view-name');

        Paginator::defaultSimpleView('view-name');
    }
}

And thatโ€™s a wrap! Now you can create and customize your own pagination views like a pro - or a comedian, we wonโ€™t judge! Happy coding! ๐Ÿ˜„๐Ÿฅณ

Rockinโ€™ with Bootstrap! ๐Ÿš€

Well, hello there, coding cowboy/cowgirl! Laravel has a secret weapon up its sleeve - a set of pagination views that would make even the most discerning CSS aficionado at Bootstrap do a double-take (or triple-tap on their phone).

Now, instead of settling for the default Tailwind views, why not shake things up with these fancy new boots? To get started, youโ€™ll need to call upon the paginatorโ€™s useBootstrapFour or useBootstrapFive methods in the boot function of your trusty steed, App\Providers\AppServiceProvider class.

Hereโ€™s a little example that should have you trotting toward success in no time:

use Illuminate\Pagination\Paginator;

/**
 * Saddle up any application services!
 */
public function boot(): void
{
    Paginator::useBootstrapFive(); // Or Paginator::useBootstrapFour(), depending on your preference
    // And away we go! ๐ŸŽ
}

Now, trot on over to paginator instance methods, and letโ€™s see if thereโ€™s any more ways you can spruce up those pagination views like a real cowpoke! ๐Ÿ˜Ž

Ahoy there, mateys! Letโ€™s dive into the magical world of Laravel Paginators, where every instance is brimming with swashbuckling secrets to help you navigate your data like a seasoned sea dog! ๐ŸŒด

MethodWhat it does
$paginator->count()Counts the number of treasure chests filled with items on the current page. Arrr!
$paginator->currentPage()Tells ye where you are in the pirateโ€™s treasure map, matey. Are we on the Gold Coast or still at the Scrubby Shore? Find out here!
$paginator->firstItem()Reveals the number of the first item ye be seeing, from one to however many there are!
$paginator->getOptions()Gives ye a peek at the secret sauce behind this pirateโ€™s treasure hunt. Whatโ€™s in the recipe? Find out here!
$paginator->getUrlRange($start, $end)Creates a list of URLs pointing to every page in our booty-filled journey. Sail from one island to another without getting lost!
$paginator->hasPages()Checks if there be enough treasure chests for multiple islands along the way. Will we have many stops or just a few? Find out here!
$paginator->hasMorePages()Checks if the sea captain has hidden more treasures for us to find! Keep digging, matey, there might be more gold ahead!
$paginator->items()Gives ye access to all the goodies on the current island. Be prepared to be amazed by your treasure trove!
$paginator->lastItem()Reveals the number of the last item on the current island. How many golden doubloons did Captain Jack hoard here? Find out now!
$paginator->lastPage()Tells ye about the last island weโ€™re headed to in this treasure hunt. What lies beyond the Gold Coast? Letโ€™s find out together!
$paginator->nextPageUrl()Gives ye the coordinates for the next island on our map, matey. Set sail and explore further into the unknown!
$paginator->onFirstPage()Checks if weโ€™ve just set foot on the first island in this treasure hunt. Are we at the Scrubby Shore or have we already plundered more?
$paginator->onLastPage()Tells ye if weโ€™re standing on the last island in our treasure hunt. Have we found all the treasure chests, or are there still hidden ones?
$paginator->perPage()Sets how many treasure chests we see per island. Do we want to take a peek at everything, or keep some secrets for ourselves? Decide now!
$paginator->previousPageUrl()Gives ye the coordinates for the last island we visited in this treasure hunt. Time to retrace our steps and explore further back!
$paginator->total()Calculates the total number of pirateโ€™s chests filled with gold and gems hidden across all islands. How much treasure have we discovered?
$paginator->url($page)Gives ye the coordinates for a specific island in our treasure hunt. Want to visit a particular island? Ask and ye shall receive!
$paginator->getPageName()Reveals the name of the variable used to store the current page number in the query string. How are we keeping track of where we be sailing?
$paginator->setPageName($name)Sets the name of the variable used to store the current page number in the query string. Want to change how we keep track of our journey? Set it here!
$paginator->through($callback)Transforms each treasure chest using a callback function, allowing ye to filter or customize your booty! Ye can add a lock and key if ye like!

Alright, letโ€™s dive into the world of Laravel cursor paginators! These little powerhouses are chock-full of tricks up their sleeves to keep your data organized and your queries tidy. Hereโ€™s a rundown of their cool party tricks:

  1. Countdown Clock ($paginator->count()): Ever wondered how many items youโ€™ve got on the current page? Just ask this guy! Itโ€™ll tell you with a cheerful โ€œTick-tock, [number] items!โ€

  2. Cursor Wrangler ($paginator->cursor()): Need to find out whoโ€™s been hanging around your data store? This method will fetch the current cursor instance without any drama.

  3. Options, Options, Options ($paginator->getOptions()): Want to know how this paginator is set up? This method will spill all the beans and tell you exactly what options itโ€™s rocking.

  4. Page Patrol ($paginator->hasPages()): This oneโ€™s like a security guard, checking if there are enough items to split into multiple pages. If the answer is yes, itโ€™ll nod and say โ€œAll clear, boss!โ€ If not, you might get a stern โ€œNope, canโ€™t split that.โ€

  5. More Items Please! ($paginator->hasMorePages()): This method is always on the lookout for more items in your data store. Itโ€™ll say โ€œHold up, thereโ€™s more where that came from!โ€ if there are, and โ€œNada, nothing new hereโ€ if there arenโ€™t.

  6. Query String Storage ($paginator->getCursorName()): This oneโ€™s like the secret service agent of the group, guarding the query string variable used to store the cursor.

  7. All You Can Eat Buffet ($paginator->items()): Need to grab the items for the current page? Think of it as a buffet line, and this method is your server, dishing out the goods.

  8. Next Stop! ($paginator->nextCursor()): Want to know whoโ€™s in charge of the next set of items? This method will introduce you to the cursor instance responsible for that duty.

  9. Destination Unknown ($paginator->nextPageUrl()): Ever needed a mystery tour to the next page? This method will provide the URL, but shrouded in intrigue with a โ€œYour next destination awaits!โ€

  10. First Impressions ($paginator->onFirstPage()): Need to find out if this paginator just arrived or has been here for a while? This method will confirm if itโ€™s on the first page or not.

  11. Last Call! ($paginator->onLastPage()): This oneโ€™s like the bouncer at a club, telling you if the paginator has reached its last call or still got more pages left to go.

  12. Number of Items Per Page ($paginator->perPage()): If youโ€™re curious about how many items this paginator shows per page, just ask! Itโ€™ll tell you without any fuss.

  13. Rewind ($paginator->previousCursor()): This method is like rewinding a tape, taking you back to the cursor instance for the previous set of items.

  14. Time Travel ($paginator->previousPageUrl()): Need to go back in time and visit the previous page? This method will provide the URL to make it happen.

  15. Cursor Renaming Service ($paginator->setCursorName()): Want to change the query string variable used to store the cursor? This methodโ€™s your new best friend!

  16. Custom Cursor Destination ($paginator->url($cursor)): If youโ€™ve got a specific cursor instance and need its URL, this methodโ€™s got you covered! Itโ€™ll whip up the URL for you in no time.

Our network