{"id":127927,"date":"2024-05-22T14:18:15","date_gmt":"2024-05-22T18:18:15","guid":{"rendered":"https:\/\/www.inmotionhosting.com\/support\/?p=127927"},"modified":"2024-05-22T14:38:45","modified_gmt":"2024-05-22T18:38:45","slug":"mastering-laravel-pagination","status":"publish","type":"post","link":"https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/mastering-laravel-pagination\/","title":{"rendered":"Mastering Laravel Pagination"},"content":{"rendered":"<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"538\" src=\"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2024\/05\/mastering-laravel-pagination-1024x538.png\" alt=\"Mastering Laravel Pagination\" class=\"wp-image-127943\" srcset=\"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2024\/05\/mastering-laravel-pagination-1024x538.png 1024w, https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2024\/05\/mastering-laravel-pagination-300x158.png 300w, https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2024\/05\/mastering-laravel-pagination-768x403.png 768w, https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2024\/05\/mastering-laravel-pagination.png 1200w\" sizes=\"auto, (min-width: 1360px) 876px, (min-width: 960px) calc(61.58vw + 51px), calc(100vw - 80px)\" \/><\/figure>\n\n\n\n<p>Pagination is a critical feature for web applications, enhancing user experience by breaking down large data sets into manageable chunks. <a href=\"https:\/\/laravel.com\" target=\"_blank\" rel=\"noreferrer noopener\">Laravel<\/a> simplifies the implementation of pagination through its built-in <code>paginate()<\/code> method. This article will guide you through the process of setting up and customizing pagination in Laravel, ensuring a smooth and efficient user experience.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"#setting-up\">Setting Up Pagination in Laravel<\/a><\/li>\n\n\n\n<li><a href=\"#basic\">Basic Usage of <code>paginate()<\/code><\/a><\/li>\n\n\n\n<li><a href=\"#customizing\">Customizing Pagination Links<\/a><\/li>\n\n\n\n<li><a href=\"#advanced\">Advanced Pagination Features<\/a><\/li>\n\n\n\n<li><a href=\"#relationships\">Pagination with Relationships<\/a><\/li>\n\n\n\n<li><a href=\"#large-data\">Handling Large Datasets<\/a><\/li>\n\n\n\n<li><a href=\"#parameters\">Customizing URL Parameters<\/a><\/li>\n\n\n\n<li><a href=\"#error\">Error Handling in Pagination<\/a><\/li>\n\n\n\n<li><a href=\"#testing\">Testing Pagination<\/a><\/li>\n\n\n\n<li><a href=\"#conclusion\">Conclusion<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading toc-anchor\" id=\"setting-up\">Setting Up Pagination in Laravel<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Installing Laravel<\/h3>\n\n\n\n<p>Before diving into pagination, ensure you have Laravel installed. If not, you can install it using Composer:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono-NL.ttf\" style=\"font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:clamp(26px, 1.625rem, 39px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"composer create-project --prefer-dist laravel\/laravel myApp\" style=\"color:#e1e4e8;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewbox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki github-dark\" style=\"background-color: #24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #B392F0\">composer<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #9ECBFF\">create-project<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">--prefer-dist<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #9ECBFF\">laravel\/laravel<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #9ECBFF\">myApp<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p class=\"alert alert-info\">Check out our <a href=\"https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/how-to-install-laravel\/\">How to Install Laravel article<\/a> for more in-depth instructions!<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Setting Up a Database and Creating a Model<\/h3>\n\n\n\n<p>Configure your <code>.env<\/code> file with your database details:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono-NL.ttf\" style=\"font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:clamp(26px, 1.625rem, 39px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"DB_CONNECTION=mysql\nDB_HOST=127.0.0.1\nDB_PORT=3306\nDB_DATABASE=your_database\nDB_USERNAME=your_username\nDB_PASSWORD=your_password\" style=\"color:#e1e4e8;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewbox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki github-dark\" style=\"background-color: #24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #79B8FF\">DB_CONNECTION<\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #79B8FF\">mysql<\/span><\/span>\n<span class=\"line\"><span style=\"color: #79B8FF\">DB_HOST<\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #79B8FF\">127.0.0.1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #79B8FF\">DB_PORT<\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #79B8FF\">3306<\/span><\/span>\n<span class=\"line\"><span style=\"color: #79B8FF\">DB_DATABASE<\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #79B8FF\">your_database<\/span><\/span>\n<span class=\"line\"><span style=\"color: #79B8FF\">DB_USERNAME<\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #79B8FF\">your_username<\/span><\/span>\n<span class=\"line\"><span style=\"color: #79B8FF\">DB_PASSWORD<\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #79B8FF\">your_password<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p class=\"alert alert-info\">Our <a href=\"https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/how-to-configure-the-laravel-env-for-a-database\/\">How to Configure the Laravel .env for a Database article <\/a>explains the process more in-depth.<\/p>\n\n\n\n<p>Run the migration to create a table:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono-NL.ttf\" style=\"font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:clamp(26px, 1.625rem, 39px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"php artisan make:migration create_posts_table --create=posts\" style=\"color:#e1e4e8;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewbox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki github-dark\" style=\"background-color: #24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #B392F0\">php<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #9ECBFF\">artisan<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #9ECBFF\">make:migration<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #9ECBFF\">create_posts_table<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">--create=posts<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>Edit the migration file to include the necessary columns:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono-NL.ttf\" style=\"font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:clamp(26px, 1.625rem, 39px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"Schema::create('posts', function (Blueprint $table) {\n    $table-&gt;id();\n    $table-&gt;string('title');\n    $table-&gt;text('body');\n    $table-&gt;timestamps();\n});\" style=\"color:#e1e4e8;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewbox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki github-dark\" style=\"background-color: #24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #79B8FF\">Schema<\/span><span style=\"color: #F97583\">::<\/span><span style=\"color: #B392F0\">create<\/span><span style=\"color: #E1E4E8\">(<\/span><span style=\"color: #9ECBFF\">'posts'<\/span><span style=\"color: #E1E4E8\">, <\/span><span style=\"color: #F97583\">function<\/span><span style=\"color: #E1E4E8\"> (<\/span><span style=\"color: #79B8FF\">Blueprint<\/span><span style=\"color: #E1E4E8\"> $table) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    $table<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">id<\/span><span style=\"color: #E1E4E8\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    $table<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">string<\/span><span style=\"color: #E1E4E8\">(<\/span><span style=\"color: #9ECBFF\">'title'<\/span><span style=\"color: #E1E4E8\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    $table<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">text<\/span><span style=\"color: #E1E4E8\">(<\/span><span style=\"color: #9ECBFF\">'body'<\/span><span style=\"color: #E1E4E8\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    $table<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">timestamps<\/span><span style=\"color: #E1E4E8\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">});<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>Run the migration:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono-NL.ttf\" style=\"font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:clamp(26px, 1.625rem, 39px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"php artisan migrate\" style=\"color:#e1e4e8;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewbox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki github-dark\" style=\"background-color: #24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #B392F0\">php<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #9ECBFF\">artisan<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #9ECBFF\">migrate<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>Create a model for the <code>posts<\/code> table:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono-NL.ttf\" style=\"font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:clamp(26px, 1.625rem, 39px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"php artisan make:model Post\" style=\"color:#e1e4e8;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewbox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki github-dark\" style=\"background-color: #24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #B392F0\">php<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #9ECBFF\">artisan<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #9ECBFF\">make:model<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #9ECBFF\">Post<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading toc-anchor\" id=\"basic\">Basic Usage of <code>paginate()<\/code><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation of the <code>paginate()<\/code> Method<\/h3>\n\n\n\n<p>The <code>paginate()<\/code> method is part of Laravel\u2019s Eloquent ORM. It simplifies fetching a limited set of records from the database and automatically generates links for navigating through the pages. It works seamlessly with Eloquent models and queries, making it easy to implement pagination in your application.<\/p>\n\n\n\n<p>The method accepts an integer parameter, which specifies the number of records to be displayed per page. For instance, <code>Post::paginate(10)<\/code> will fetch 10 records per page from the <code>posts<\/code> table.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example: Paginating a List of Records<\/h3>\n\n\n\n<p>Let\u2019s go through a step-by-step example of paginating a list of records.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Step 1: Create a Controller<\/h4>\n\n\n\n<p>First, create a controller to handle the logic. You can use the Artisan command to generate the controller:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono-NL.ttf\" style=\"font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:clamp(26px, 1.625rem, 39px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"php artisan make:controller PostController\" style=\"color:#e1e4e8;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewbox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki github-dark\" style=\"background-color: #24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #B392F0\">php<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #9ECBFF\">artisan<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #9ECBFF\">make:controller<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #9ECBFF\">PostController<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>Open the newly created <code>PostController<\/code> and add the following code to the <code>index<\/code> method:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono-NL.ttf\" style=\"font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:clamp(26px, 1.625rem, 39px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"namespace App\\Http\\Controllers;\n\nuse App\\Models\\Post;\nuse Illuminate\\Http\\Request;\n\nclass PostController extends Controller\n{\n    public function index()\n    {\n        $posts = Post::paginate(10);\n        return view('posts.index', compact('posts'));\n    }\n}\" style=\"color:#e1e4e8;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewbox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki github-dark\" style=\"background-color: #24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F97583\">namespace<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">App\\Http\\Controllers<\/span><span style=\"color: #E1E4E8\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F97583\">use<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">App\\Models\\Post<\/span><span style=\"color: #E1E4E8\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F97583\">use<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">Illuminate\\Http\\Request<\/span><span style=\"color: #E1E4E8\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F97583\">class<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">PostController<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #F97583\">extends<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">Controller<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    <\/span><span style=\"color: #F97583\">public<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #F97583\">function<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">index<\/span><span style=\"color: #E1E4E8\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        $posts <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">Post<\/span><span style=\"color: #F97583\">::<\/span><span style=\"color: #B392F0\">paginate<\/span><span style=\"color: #E1E4E8\">(<\/span><span style=\"color: #79B8FF\">10<\/span><span style=\"color: #E1E4E8\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">return<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">view<\/span><span style=\"color: #E1E4E8\">(<\/span><span style=\"color: #9ECBFF\">'posts.index'<\/span><span style=\"color: #E1E4E8\">, <\/span><span style=\"color: #79B8FF\">compact<\/span><span style=\"color: #E1E4E8\">(<\/span><span style=\"color: #9ECBFF\">'posts'<\/span><span style=\"color: #E1E4E8\">));<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>In this code, <code>Post::paginate(10)<\/code> fetches 10 records from the <code>posts<\/code> table. The resulting <code>Paginator<\/code> instance is passed to the view named <code>posts.index<\/code>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Step 2: Create a Route<\/h4>\n\n\n\n<p>Next, define a route that maps to the <code>index<\/code> method of the <code>PostController<\/code>. Open the <code>routes\/web.php<\/code> file and add the following line:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono-NL.ttf\" style=\"font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:clamp(26px, 1.625rem, 39px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"use App\\Http\\Controllers\\PostController;\n\nRoute::get('\/posts', [PostController::class, 'index']);\" style=\"color:#e1e4e8;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewbox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki github-dark\" style=\"background-color: #24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F97583\">use<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">App\\Http\\Controllers\\PostController<\/span><span style=\"color: #E1E4E8\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #79B8FF\">Route<\/span><span style=\"color: #F97583\">::<\/span><span style=\"color: #B392F0\">get<\/span><span style=\"color: #E1E4E8\">(<\/span><span style=\"color: #9ECBFF\">'\/posts'<\/span><span style=\"color: #E1E4E8\">, [<\/span><span style=\"color: #79B8FF\">PostController<\/span><span style=\"color: #F97583\">::class<\/span><span style=\"color: #E1E4E8\">, <\/span><span style=\"color: #9ECBFF\">'index'<\/span><span style=\"color: #E1E4E8\">]);<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>This route will handle GET requests to <code>\/posts<\/code> and invoke the <code>index<\/code> method of <code>PostController<\/code>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Step 3: Create a Blade Template<\/h4>\n\n\n\n<p>Create a Blade template to display the paginated results. Create a new file named <code>index.blade.php<\/code> inside the <code>resources\/views\/posts<\/code> directory:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono-NL.ttf\" style=\"font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:clamp(26px, 1.625rem, 39px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n&lt;head&gt;\n    &lt;title&gt;Laravel Pagination&lt;\/title&gt;\n    &lt;link rel=&quot;stylesheet&quot; href=&quot;&quot; \/&gt;\n    &lt;script src=&quot;https:\/\/cdn.tailwindcss.com&quot; &gt;&lt;\/script&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n&lt;div class=&quot;container mt-5&quot;&gt;\n    &lt;h1&gt;Laravel Pagination Example&lt;\/h1&gt;\n    &lt;table class=&quot;table table-bordered&quot;&gt;\n        &lt;thead&gt;\n            &lt;tr&gt;\n                &lt;th&gt;ID&lt;\/th&gt;\n                &lt;th&gt;Title&lt;\/th&gt;\n                &lt;th&gt;Body&lt;\/th&gt;\n            &lt;\/tr&gt;\n        &lt;\/thead&gt;\n        &lt;tbody&gt;\n            @foreach($posts as $post)\n            &lt;tr&gt;\n                &lt;td&gt;{{ $post-&gt;id }}&lt;\/td&gt;\n                &lt;td&gt;{{ $post-&gt;title }}&lt;\/td&gt;\n                &lt;td&gt;{{ $post-&gt;body }}&lt;\/td&gt;\n            &lt;\/tr&gt;\n            @endforeach\n        &lt;\/tbody&gt;\n    &lt;\/table&gt;\n    {!! $posts-&gt;links() !!}\n&lt;\/div&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\" style=\"color:#e1e4e8;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewbox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki github-dark\" style=\"background-color: #24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F97583\">&lt;!<\/span><span style=\"color: #79B8FF\">DOCTYPE<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">html<\/span><span style=\"color: #F97583\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F97583\">&lt;<\/span><span style=\"color: #79B8FF\">html<\/span><span style=\"color: #F97583\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F97583\">&lt;<\/span><span style=\"color: #79B8FF\">head<\/span><span style=\"color: #F97583\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    <\/span><span style=\"color: #F97583\">&lt;<\/span><span style=\"color: #79B8FF\">title<\/span><span style=\"color: #F97583\">&gt;<\/span><span style=\"color: #79B8FF\">Laravel<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">Pagination<\/span><span style=\"color: #F97583\">&lt;\/<\/span><span style=\"color: #79B8FF\">title<\/span><span style=\"color: #F97583\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    <\/span><span style=\"color: #F97583\">&lt;<\/span><span style=\"color: #79B8FF\">link<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">rel<\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #9ECBFF\">\"stylesheet\"<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">href<\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #9ECBFF\">\"\"<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #F97583\">\/&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    <\/span><span style=\"color: #F97583\">&lt;<\/span><span style=\"color: #79B8FF\">script<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">src<\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #9ECBFF\">\"https:\/\/cdn.tailwindcss.com\"<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #F97583\">&gt;&lt;\/<\/span><span style=\"color: #79B8FF\">script<\/span><span style=\"color: #F97583\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F97583\">&lt;\/<\/span><span style=\"color: #79B8FF\">head<\/span><span style=\"color: #F97583\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F97583\">&lt;<\/span><span style=\"color: #79B8FF\">body<\/span><span style=\"color: #F97583\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F97583\">&lt;<\/span><span style=\"color: #79B8FF\">div<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #F97583\">class=<\/span><span style=\"color: #9ECBFF\">\"container mt-5\"<\/span><span style=\"color: #F97583\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    <\/span><span style=\"color: #F97583\">&lt;<\/span><span style=\"color: #79B8FF\">h1<\/span><span style=\"color: #F97583\">&gt;<\/span><span style=\"color: #79B8FF\">Laravel<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">Pagination<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">Example<\/span><span style=\"color: #F97583\">&lt;\/<\/span><span style=\"color: #79B8FF\">h1<\/span><span style=\"color: #F97583\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    <\/span><span style=\"color: #F97583\">&lt;<\/span><span style=\"color: #79B8FF\">table<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #F97583\">class=<\/span><span style=\"color: #9ECBFF\">\"table table-bordered\"<\/span><span style=\"color: #F97583\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">&lt;<\/span><span style=\"color: #79B8FF\">thead<\/span><span style=\"color: #F97583\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            <\/span><span style=\"color: #F97583\">&lt;<\/span><span style=\"color: #79B8FF\">tr<\/span><span style=\"color: #F97583\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                <\/span><span style=\"color: #F97583\">&lt;<\/span><span style=\"color: #79B8FF\">th<\/span><span style=\"color: #F97583\">&gt;<\/span><span style=\"color: #79B8FF\">ID<\/span><span style=\"color: #F97583\">&lt;\/<\/span><span style=\"color: #79B8FF\">th<\/span><span style=\"color: #F97583\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                <\/span><span style=\"color: #F97583\">&lt;<\/span><span style=\"color: #79B8FF\">th<\/span><span style=\"color: #F97583\">&gt;<\/span><span style=\"color: #79B8FF\">Title<\/span><span style=\"color: #F97583\">&lt;\/<\/span><span style=\"color: #79B8FF\">th<\/span><span style=\"color: #F97583\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                <\/span><span style=\"color: #F97583\">&lt;<\/span><span style=\"color: #79B8FF\">th<\/span><span style=\"color: #F97583\">&gt;<\/span><span style=\"color: #79B8FF\">Body<\/span><span style=\"color: #F97583\">&lt;\/<\/span><span style=\"color: #79B8FF\">th<\/span><span style=\"color: #F97583\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            <\/span><span style=\"color: #F97583\">&lt;\/<\/span><span style=\"color: #79B8FF\">tr<\/span><span style=\"color: #F97583\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">&lt;\/<\/span><span style=\"color: #79B8FF\">thead<\/span><span style=\"color: #F97583\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">&lt;<\/span><span style=\"color: #79B8FF\">tbody<\/span><span style=\"color: #F97583\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            <\/span><span style=\"color: #F97583\">@foreach<\/span><span style=\"color: #E1E4E8\">($posts <\/span><span style=\"color: #F97583\">as<\/span><span style=\"color: #E1E4E8\"> $post)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            <\/span><span style=\"color: #F97583\">&lt;<\/span><span style=\"color: #79B8FF\">tr<\/span><span style=\"color: #F97583\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                <\/span><span style=\"color: #F97583\">&lt;<\/span><span style=\"color: #79B8FF\">td<\/span><span style=\"color: #F97583\">&gt;<\/span><span style=\"color: #E1E4E8\">{{ $post<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #E1E4E8\">id }}<\/span><span style=\"color: #F97583\">&lt;\/<\/span><span style=\"color: #79B8FF\">td<\/span><span style=\"color: #F97583\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                <\/span><span style=\"color: #F97583\">&lt;<\/span><span style=\"color: #79B8FF\">td<\/span><span style=\"color: #F97583\">&gt;<\/span><span style=\"color: #E1E4E8\">{{ $post<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #E1E4E8\">title }}<\/span><span style=\"color: #F97583\">&lt;\/<\/span><span style=\"color: #79B8FF\">td<\/span><span style=\"color: #F97583\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                <\/span><span style=\"color: #F97583\">&lt;<\/span><span style=\"color: #79B8FF\">td<\/span><span style=\"color: #F97583\">&gt;<\/span><span style=\"color: #E1E4E8\">{{ $post<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #E1E4E8\">body }}<\/span><span style=\"color: #F97583\">&lt;\/<\/span><span style=\"color: #79B8FF\">td<\/span><span style=\"color: #F97583\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            <\/span><span style=\"color: #F97583\">&lt;\/<\/span><span style=\"color: #79B8FF\">tr<\/span><span style=\"color: #F97583\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            <\/span><span style=\"color: #F97583\">@endforeach<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">&lt;\/<\/span><span style=\"color: #79B8FF\">tbody<\/span><span style=\"color: #F97583\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    <\/span><span style=\"color: #F97583\">&lt;\/<\/span><span style=\"color: #79B8FF\">table<\/span><span style=\"color: #F97583\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    {<\/span><span style=\"color: #F97583\">!!<\/span><span style=\"color: #E1E4E8\"> $posts<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">links<\/span><span style=\"color: #E1E4E8\">() <\/span><span style=\"color: #F97583\">!!<\/span><span style=\"color: #E1E4E8\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F97583\">&lt;\/<\/span><span style=\"color: #79B8FF\">div<\/span><span style=\"color: #F97583\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F97583\">&lt;\/<\/span><span style=\"color: #79B8FF\">body<\/span><span style=\"color: #F97583\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F97583\">&lt;\/<\/span><span style=\"color: #79B8FF\">html<\/span><span style=\"color: #F97583\">&gt;<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">Step 4: Displaying Paginated Results<\/h4>\n\n\n\n<p>In the Blade template, we loop through the <code>$posts<\/code> collection and display each post\u2019s ID, title, and body in a table. The <code>{!! $posts-&gt;links() !!}<\/code> directive renders the pagination links, which are automatically generated by Laravel.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"366\" src=\"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2024\/05\/image-9-1024x366.png\" alt=\"\" class=\"wp-image-127931\" srcset=\"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2024\/05\/image-9-1024x366.png 1024w, https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2024\/05\/image-9-300x107.png 300w, https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2024\/05\/image-9-768x274.png 768w, https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2024\/05\/image-9.png 1299w\" sizes=\"auto, (min-width: 1360px) 876px, (min-width: 960px) calc(61.58vw + 51px), calc(100vw - 80px)\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Example Breakdown<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Controller<\/strong>: Fetches paginated data from the database using <code>paginate()<\/code>.<\/li>\n\n\n\n<li><strong>Route<\/strong>: Defines a URL that triggers the controller method.<\/li>\n\n\n\n<li><strong>Blade Template<\/strong>: Displays the paginated data and renders the pagination links.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading toc-anchor\" id=\"customizing\">Customizing Pagination Links<\/h2>\n\n\n\n<p>Laravel uses the Tailwind CSS framework by default for pagination styling. If you\u2019re using a different CSS framework or want to customize the appearance of pagination links, you can publish the pagination views and modify them.<\/p>\n\n\n\n<p>Publish the pagination views:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono-NL.ttf\" style=\"font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:clamp(26px, 1.625rem, 39px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"php artisan vendor:publish --tag=laravel-pagination\" style=\"color:#e1e4e8;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewbox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki github-dark\" style=\"background-color: #24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #79B8FF\">php<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">artisan<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">vendor<\/span><span style=\"color: #E1E4E8\">:<\/span><span style=\"color: #79B8FF\">publish<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #F97583\">--<\/span><span style=\"color: #79B8FF\">tag<\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #79B8FF\">laravel<\/span><span style=\"color: #F97583\">-<\/span><span style=\"color: #79B8FF\">pagination<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>This command copies the pagination views to <code>resources\/views\/vendor\/pagination<\/code>, where you can edit them as needed.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Changing the Number of Items Per Page<\/h3>\n\n\n\n<p>You can easily change the number of items displayed per page by passing a different value to the <code>paginate()<\/code> method:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono-NL.ttf\" style=\"font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:clamp(26px, 1.625rem, 39px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"$posts = Post::paginate(20);\" style=\"color:#e1e4e8;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewbox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki github-dark\" style=\"background-color: #24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #E1E4E8\">$posts <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">Post<\/span><span style=\"color: #F97583\">::<\/span><span style=\"color: #B392F0\">paginate<\/span><span style=\"color: #E1E4E8\">(<\/span><span style=\"color: #79B8FF\">20<\/span><span style=\"color: #E1E4E8\">);<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"564\" src=\"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2024\/05\/image-10-1024x564.png\" alt=\"\" class=\"wp-image-127934\" srcset=\"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2024\/05\/image-10-1024x564.png 1024w, https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2024\/05\/image-10-300x165.png 300w, https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2024\/05\/image-10-768x423.png 768w, https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2024\/05\/image-10.png 1278w\" sizes=\"auto, (min-width: 1360px) 876px, (min-width: 960px) calc(61.58vw + 51px), calc(100vw - 80px)\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Customizing the Pagination View<\/h3>\n\n\n\n<p>Laravel allows you to customize the appearance of pagination links by using different views. You can specify the view to be used with the <code>links()<\/code> method:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono-NL.ttf\" style=\"font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:clamp(26px, 1.625rem, 39px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"{!! $posts-&gt;links('vendor.pagination.bootstrap-4') !!}\" style=\"color:#e1e4e8;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewbox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki github-dark\" style=\"background-color: #24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #E1E4E8\">{<\/span><span style=\"color: #F97583\">!!<\/span><span style=\"color: #E1E4E8\"> $posts<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">links<\/span><span style=\"color: #E1E4E8\">(<\/span><span style=\"color: #9ECBFF\">'vendor.pagination.bootstrap-4'<\/span><span style=\"color: #E1E4E8\">) <\/span><span style=\"color: #F97583\">!!<\/span><span style=\"color: #E1E4E8\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>You can create your custom pagination view in the <code>resources\/views\/vendor\/pagination<\/code> directory if needed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading toc-anchor\" id=\"advanced\">Advanced Pagination Features<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Using <code>simplePaginate()<\/code><\/h3>\n\n\n\n<p>The <code>simplePaginate()<\/code> method is a lightweight alternative to <code>paginate()<\/code>, providing a simple \u201cNext\u201d and \u201cPrevious\u201d navigation without calculating the total number of pages. It\u2019s useful for large datasets where performance is a concern.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono-NL.ttf\" style=\"font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:clamp(26px, 1.625rem, 39px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"$posts = Post::simplePaginate(10);\" style=\"color:#e1e4e8;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewbox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki github-dark\" style=\"background-color: #24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #E1E4E8\">$posts <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">Post<\/span><span style=\"color: #F97583\">::<\/span><span style=\"color: #B392F0\">simplePaginate<\/span><span style=\"color: #E1E4E8\">(<\/span><span style=\"color: #79B8FF\">10<\/span><span style=\"color: #E1E4E8\">);<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"360\" src=\"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2024\/05\/image-11-1024x360.png\" alt=\"\" class=\"wp-image-127936\" srcset=\"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2024\/05\/image-11-1024x360.png 1024w, https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2024\/05\/image-11-300x105.png 300w, https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2024\/05\/image-11-768x270.png 768w, https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2024\/05\/image-11.png 1317w\" sizes=\"auto, (min-width: 1360px) 876px, (min-width: 960px) calc(61.58vw + 51px), calc(100vw - 80px)\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Adding Filters to Paginated Results<\/h3>\n\n\n\n<p>You can add filters to your paginated results by modifying the query before calling the <code>paginate()<\/code> method:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono-NL.ttf\" style=\"font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:clamp(26px, 1.625rem, 39px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"$posts = Post::where('category', 'Tech')-&gt;paginate(10);\" style=\"color:#e1e4e8;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewbox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki github-dark\" style=\"background-color: #24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #E1E4E8\">$posts <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">Post<\/span><span style=\"color: #F97583\">::<\/span><span style=\"color: #B392F0\">where<\/span><span style=\"color: #E1E4E8\">(<\/span><span style=\"color: #9ECBFF\">'category'<\/span><span style=\"color: #E1E4E8\">, <\/span><span style=\"color: #9ECBFF\">'Tech'<\/span><span style=\"color: #E1E4E8\">)<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">paginate<\/span><span style=\"color: #E1E4E8\">(<\/span><span style=\"color: #79B8FF\">10<\/span><span style=\"color: #E1E4E8\">);<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading toc-anchor\" id=\"relationships\">Pagination with Relationships<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Paginating Related Models<\/h3>\n\n\n\n<p>Paginating related models is straightforward with Laravel. For example, to paginate comments for a post:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono-NL.ttf\" style=\"font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:clamp(26px, 1.625rem, 39px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"$post = Post::find($id);\n$comments = $post-&gt;comments()-&gt;paginate(10);\n\nreturn view('posts.comments', compact('post', 'comments'));\" style=\"color:#e1e4e8;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewbox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki github-dark\" style=\"background-color: #24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #E1E4E8\">$post <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">Post<\/span><span style=\"color: #F97583\">::<\/span><span style=\"color: #B392F0\">find<\/span><span style=\"color: #E1E4E8\">($id);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">$comments <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> $post<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">comments<\/span><span style=\"color: #E1E4E8\">()<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">paginate<\/span><span style=\"color: #E1E4E8\">(<\/span><span style=\"color: #79B8FF\">10<\/span><span style=\"color: #E1E4E8\">);<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F97583\">return<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">view<\/span><span style=\"color: #E1E4E8\">(<\/span><span style=\"color: #9ECBFF\">'posts.comments'<\/span><span style=\"color: #E1E4E8\">, <\/span><span style=\"color: #79B8FF\">compact<\/span><span style=\"color: #E1E4E8\">(<\/span><span style=\"color: #9ECBFF\">'post'<\/span><span style=\"color: #E1E4E8\">, <\/span><span style=\"color: #9ECBFF\">'comments'<\/span><span style=\"color: #E1E4E8\">));<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>In your Blade template, display the comments and pagination links:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono-NL.ttf\" style=\"font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:clamp(26px, 1.625rem, 39px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"@foreach($comments as $comment)\n    &lt;p&gt;{{ $comment-&gt;body }}&lt;\/p&gt;\n@endforeach\n\n{!! $comments-&gt;links() !!}\" style=\"color:#e1e4e8;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewbox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki github-dark\" style=\"background-color: #24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F97583\">@foreach<\/span><span style=\"color: #E1E4E8\">($comments <\/span><span style=\"color: #F97583\">as<\/span><span style=\"color: #E1E4E8\"> $comment)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    <\/span><span style=\"color: #F97583\">&lt;<\/span><span style=\"color: #79B8FF\">p<\/span><span style=\"color: #F97583\">&gt;<\/span><span style=\"color: #E1E4E8\">{{ $comment<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #E1E4E8\">body }}<\/span><span style=\"color: #F97583\">&lt;\/<\/span><span style=\"color: #79B8FF\">p<\/span><span style=\"color: #F97583\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F97583\">@endforeach<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">{<\/span><span style=\"color: #F97583\">!!<\/span><span style=\"color: #E1E4E8\"> $comments<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">links<\/span><span style=\"color: #E1E4E8\">() <\/span><span style=\"color: #F97583\">!!<\/span><span style=\"color: #E1E4E8\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading toc-anchor\" id=\"large-data\">Handling Large Datasets<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Efficient Pagination Strategies for Large Datasets<\/h3>\n\n\n\n<p>For very large datasets, <code>cursorPaginate()<\/code> offers a more efficient approach by using database cursors instead of offset\/limit pagination.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono-NL.ttf\" style=\"font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:clamp(26px, 1.625rem, 39px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"$posts = Post::cursorPaginate(10);\" style=\"color:#e1e4e8;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewbox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki github-dark\" style=\"background-color: #24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #E1E4E8\">$posts <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">Post<\/span><span style=\"color: #F97583\">::<\/span><span style=\"color: #B392F0\">cursorPaginate<\/span><span style=\"color: #E1E4E8\">(<\/span><span style=\"color: #79B8FF\">10<\/span><span style=\"color: #E1E4E8\">);<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>This method reduces the overhead of counting the total number of records.<\/p>\n\n\n\n<h2 class=\"wp-block-heading toc-anchor\" id=\"parameters\">Customizing URL Parameters<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Customizing Query String Parameters for Pagination<\/h3>\n\n\n\n<p>You can customize the query string parameters used in pagination links. For example, to append custom parameters:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono-NL.ttf\" style=\"font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:clamp(26px, 1.625rem, 39px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"$posts = Post::paginate(10)-&gt;appends(['sort' =&gt; 'title']);\" style=\"color:#e1e4e8;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewbox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki github-dark\" style=\"background-color: #24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #E1E4E8\">$posts <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">Post<\/span><span style=\"color: #F97583\">::<\/span><span style=\"color: #B392F0\">paginate<\/span><span style=\"color: #E1E4E8\">(<\/span><span style=\"color: #79B8FF\">10<\/span><span style=\"color: #E1E4E8\">)<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">appends<\/span><span style=\"color: #E1E4E8\">([<\/span><span style=\"color: #9ECBFF\">'sort'<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #F97583\">=&gt;<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #9ECBFF\">'title'<\/span><span style=\"color: #E1E4E8\">]);<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Customizing the Page Parameter<\/h3>\n\n\n\n<p>You can also customize the page parameter if needed:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono-NL.ttf\" style=\"font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:clamp(26px, 1.625rem, 39px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"$posts = Post::paginate(10)-&gt;withPath('custom-page');\" style=\"color:#e1e4e8;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewbox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki github-dark\" style=\"background-color: #24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #E1E4E8\">$posts <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">Post<\/span><span style=\"color: #F97583\">::<\/span><span style=\"color: #B392F0\">paginate<\/span><span style=\"color: #E1E4E8\">(<\/span><span style=\"color: #79B8FF\">10<\/span><span style=\"color: #E1E4E8\">)<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">withPath<\/span><span style=\"color: #E1E4E8\">(<\/span><span style=\"color: #9ECBFF\">'custom-page'<\/span><span style=\"color: #E1E4E8\">);<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading toc-anchor\" id=\"error\">Error Handling in Pagination<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Handling Empty Pages<\/h3>\n\n\n\n<p>When dealing with pagination, you might encounter empty pages. Redirecting users to a valid page or displaying a custom message can enhance the user experience:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono-NL.ttf\" style=\"font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:clamp(26px, 1.625rem, 39px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"public function index(Request $request)\n{\n    $page = $request-&gt;input('page', 1);\n    $posts = Post::paginate(10);\n\n    if ($posts-&gt;isEmpty() &amp;&amp; $page &gt; 1) {\n        return redirect()-&gt;route('posts.index', ['page' =&gt; $posts-&gt;lastPage()]);\n    }\n\n    return view('posts.index', compact('posts'));\n}\" style=\"color:#e1e4e8;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewbox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki github-dark\" style=\"background-color: #24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F97583\">public<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #F97583\">function<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">index<\/span><span style=\"color: #E1E4E8\">(<\/span><span style=\"color: #79B8FF\">Request<\/span><span style=\"color: #E1E4E8\"> $request)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    $page <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> $request<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">input<\/span><span style=\"color: #E1E4E8\">(<\/span><span style=\"color: #9ECBFF\">'page'<\/span><span style=\"color: #E1E4E8\">, <\/span><span style=\"color: #79B8FF\">1<\/span><span style=\"color: #E1E4E8\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    $posts <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">Post<\/span><span style=\"color: #F97583\">::<\/span><span style=\"color: #B392F0\">paginate<\/span><span style=\"color: #E1E4E8\">(<\/span><span style=\"color: #79B8FF\">10<\/span><span style=\"color: #E1E4E8\">);<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    <\/span><span style=\"color: #F97583\">if<\/span><span style=\"color: #E1E4E8\"> ($posts<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">isEmpty<\/span><span style=\"color: #E1E4E8\">() <\/span><span style=\"color: #F97583\">&amp;&amp;<\/span><span style=\"color: #E1E4E8\"> $page <\/span><span style=\"color: #F97583\">&gt;<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">1<\/span><span style=\"color: #E1E4E8\">) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">return<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">redirect<\/span><span style=\"color: #E1E4E8\">()<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">route<\/span><span style=\"color: #E1E4E8\">(<\/span><span style=\"color: #9ECBFF\">'posts.index'<\/span><span style=\"color: #E1E4E8\">, [<\/span><span style=\"color: #9ECBFF\">'page'<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #F97583\">=&gt;<\/span><span style=\"color: #E1E4E8\"> $posts<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">lastPage<\/span><span style=\"color: #E1E4E8\">()]);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    }<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    <\/span><span style=\"color: #F97583\">return<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">view<\/span><span style=\"color: #E1E4E8\">(<\/span><span style=\"color: #9ECBFF\">'posts.index'<\/span><span style=\"color: #E1E4E8\">, <\/span><span style=\"color: #79B8FF\">compact<\/span><span style=\"color: #E1E4E8\">(<\/span><span style=\"color: #9ECBFF\">'posts'<\/span><span style=\"color: #E1E4E8\">));<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Handling Invalid Page Numbers<\/h3>\n\n\n\n<p>Handling invalid page numbers ensures users are not stuck on a non-existent page:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono-NL.ttf\" style=\"font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:clamp(26px, 1.625rem, 39px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"public function index(Request $request)\n{\n    $page = $request-&gt;input('page', 1);\n    $posts = Post::paginate(10);\n\n    if ($page &gt; $posts-&gt;lastPage()) {\n        abort(404);\n    }\n\n    return view('posts.index', compact('posts'));\n}\" style=\"color:#e1e4e8;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewbox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki github-dark\" style=\"background-color: #24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F97583\">public<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #F97583\">function<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">index<\/span><span style=\"color: #E1E4E8\">(<\/span><span style=\"color: #79B8FF\">Request<\/span><span style=\"color: #E1E4E8\"> $request)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    $page <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> $request<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">input<\/span><span style=\"color: #E1E4E8\">(<\/span><span style=\"color: #9ECBFF\">'page'<\/span><span style=\"color: #E1E4E8\">, <\/span><span style=\"color: #79B8FF\">1<\/span><span style=\"color: #E1E4E8\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    $posts <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">Post<\/span><span style=\"color: #F97583\">::<\/span><span style=\"color: #B392F0\">paginate<\/span><span style=\"color: #E1E4E8\">(<\/span><span style=\"color: #79B8FF\">10<\/span><span style=\"color: #E1E4E8\">);<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    <\/span><span style=\"color: #F97583\">if<\/span><span style=\"color: #E1E4E8\"> ($page <\/span><span style=\"color: #F97583\">&gt;<\/span><span style=\"color: #E1E4E8\"> $posts<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">lastPage<\/span><span style=\"color: #E1E4E8\">()) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #B392F0\">abort<\/span><span style=\"color: #E1E4E8\">(<\/span><span style=\"color: #79B8FF\">404<\/span><span style=\"color: #E1E4E8\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    }<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    <\/span><span style=\"color: #F97583\">return<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">view<\/span><span style=\"color: #E1E4E8\">(<\/span><span style=\"color: #9ECBFF\">'posts.index'<\/span><span style=\"color: #E1E4E8\">, <\/span><span style=\"color: #79B8FF\">compact<\/span><span style=\"color: #E1E4E8\">(<\/span><span style=\"color: #9ECBFF\">'posts'<\/span><span style=\"color: #E1E4E8\">));<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading toc-anchor\" id=\"testing\">Testing Pagination<\/h2>\n\n\n\n<p>Testing pagination ensures your implementation works as expected. Here\u2019s how to write tests for pagination:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono-NL.ttf\" style=\"font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:clamp(26px, 1.625rem, 39px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"public function testPaginationDisplaysCorrectNumberOfItems()\n{\n    $response = $this-&gt;get('\/posts?page=1');\n    $response-&gt;assertStatus(200);\n    $response-&gt;assertSeeText('10 items');\n}\n\npublic function testPaginationLinks()\n{\n    $response = $this-&gt;get('\/posts?page=1');\n    $response-&gt;assertStatus(200);\n    $response-&gt;assertSee('&lt;nav&gt;');\n}\" style=\"color:#e1e4e8;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewbox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki github-dark\" style=\"background-color: #24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F97583\">public<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #F97583\">function<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">testPaginationDisplaysCorrectNumberOfItems<\/span><span style=\"color: #E1E4E8\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    $response <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">$this<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">get<\/span><span style=\"color: #E1E4E8\">(<\/span><span style=\"color: #9ECBFF\">'\/posts?page=1'<\/span><span style=\"color: #E1E4E8\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    $response<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">assertStatus<\/span><span style=\"color: #E1E4E8\">(<\/span><span style=\"color: #79B8FF\">200<\/span><span style=\"color: #E1E4E8\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    $response<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">assertSeeText<\/span><span style=\"color: #E1E4E8\">(<\/span><span style=\"color: #9ECBFF\">'10 items'<\/span><span style=\"color: #E1E4E8\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F97583\">public<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #F97583\">function<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">testPaginationLinks<\/span><span style=\"color: #E1E4E8\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    $response <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">$this<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">get<\/span><span style=\"color: #E1E4E8\">(<\/span><span style=\"color: #9ECBFF\">'\/posts?page=1'<\/span><span style=\"color: #E1E4E8\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    $response<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">assertStatus<\/span><span style=\"color: #E1E4E8\">(<\/span><span style=\"color: #79B8FF\">200<\/span><span style=\"color: #E1E4E8\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    $response<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">assertSee<\/span><span style=\"color: #E1E4E8\">(<\/span><span style=\"color: #9ECBFF\">'&lt;nav&gt;'<\/span><span style=\"color: #E1E4E8\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading toc-anchor\" id=\"conclusion\">Conclusion<\/h2>\n\n\n\n<p>In this article, we\u2019ve explored how to use Laravel\u2019s <code>paginate()<\/code> method to efficiently handle large datasets and enhance user experience. By understanding the basics and advanced features of pagination, you can customize and optimize it for your application\u2019s needs. For further reading, check out Laravel\u2019s <a href=\"https:\/\/laravel.com\/docs\/8.x\/pagination\">official documentation<\/a> and explore additional customization options.<\/p>\n\n\n\n<p>Implementing pagination ensures your users can navigate through data effortlessly, providing a smooth and engaging experience. Happy coding!<\/p>\n\n\n<div class=\"jumbotron\">\r\n<p>Boost your Laravel apps with our specialized <a href=\"https:\/\/www.inmotionhosting.com\/laravel-hosting\">Laravel Hosting<\/a>. Experience faster speeds for your Laravel applications and websites thanks to NVMe storage, server protection, dedicated resources, and optimization tools.<\/p>\r\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/design.inmotionhosting.com\/assets\/icons\/standard\/check-blue.svg\" alt=\"check mark\" width=\"24\" height=\"24\" \/>99.99% Uptime    <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/design.inmotionhosting.com\/assets\/icons\/standard\/check-blue.svg\" alt=\"check mark\" width=\"24\" height=\"24\" \/>Free SSL    <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/design.inmotionhosting.com\/assets\/icons\/standard\/check-blue.svg\" alt=\"check mark\" width=\"24\" height=\"24\" \/>Dedicated IP Address    <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/design.inmotionhosting.com\/assets\/icons\/standard\/check-blue.svg\" alt=\"check mark\" width=\"24\" height=\"24\" \/>Developer Tools<\/p>\r\n<p><a class=\"btn btn-primary btn-lg\" href=\"https:\/\/www.inmotionhosting.com\/laravel-hosting\">Laravel Hosting<\/a><\/p>\r\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Pagination is a critical feature for web applications, enhancing user experience by breaking down large data sets into manageable chunks. Laravel simplifies the implementation of pagination through its built-in paginate() method. This article will guide you through the process of setting up and customizing pagination in Laravel, ensuring a smooth and efficient user experience. Setting<a class=\"moretag\" href=\"https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/mastering-laravel-pagination\/\"> Read More ><\/a><\/p>\n","protected":false},"author":57032,"featured_media":127943,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4486],"tags":[],"class_list":["post-127927","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-laravel"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.1.1 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Mastering Laravel Pagination | InMotion Hosting<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/mastering-laravel-pagination\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Mastering Laravel Pagination | InMotion Hosting\" \/>\n<meta property=\"og:description\" content=\"Pagination is a critical feature for web applications, enhancing user experience by breaking down large data sets into manageable chunks. Laravel simplifies the implementation of pagination through its built-in paginate() method. This article will guide you through the process of setting up and customizing pagination in Laravel, ensuring a smooth and efficient user experience. Setting Read More &gt;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/mastering-laravel-pagination\/\" \/>\n<meta property=\"og:site_name\" content=\"InMotion Hosting Support Center\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/inmotionhosting\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-05-22T18:18:15+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-05-22T18:38:45+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2024\/05\/mastering-laravel-pagination.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Derrell\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@InMotionHosting\" \/>\n<meta name=\"twitter:site\" content=\"@InMotionHosting\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Derrell\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/mastering-laravel-pagination\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/mastering-laravel-pagination\/\"},\"author\":{\"name\":\"Derrell\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/person\/0736f70b4077032374f89709cdc255b7\"},\"headline\":\"Mastering Laravel Pagination\",\"datePublished\":\"2024-05-22T18:18:15+00:00\",\"dateModified\":\"2024-05-22T18:38:45+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/mastering-laravel-pagination\/\"},\"wordCount\":888,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/mastering-laravel-pagination\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2024\/05\/mastering-laravel-pagination.png\",\"articleSection\":[\"Laravel\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/mastering-laravel-pagination\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/mastering-laravel-pagination\/\",\"url\":\"https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/mastering-laravel-pagination\/\",\"name\":\"Mastering Laravel Pagination | InMotion Hosting\",\"isPartOf\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/mastering-laravel-pagination\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/mastering-laravel-pagination\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2024\/05\/mastering-laravel-pagination.png\",\"datePublished\":\"2024-05-22T18:18:15+00:00\",\"dateModified\":\"2024-05-22T18:38:45+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/mastering-laravel-pagination\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/mastering-laravel-pagination\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/mastering-laravel-pagination\/#primaryimage\",\"url\":\"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2024\/05\/mastering-laravel-pagination.png\",\"contentUrl\":\"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2024\/05\/mastering-laravel-pagination.png\",\"width\":1200,\"height\":630},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/mastering-laravel-pagination\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.inmotionhosting.com\/support\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Mastering Laravel Pagination\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#website\",\"url\":\"https:\/\/www.inmotionhosting.com\/support\/\",\"name\":\"InMotion Hosting Support Center\",\"description\":\"Web Hosting Support &amp; Tutorials\",\"publisher\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.inmotionhosting.com\/support\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#organization\",\"name\":\"InMotion Hosting\",\"url\":\"https:\/\/www.inmotionhosting.com\/support\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2023\/02\/inmotion-hosting-logo-yoast.jpg\",\"contentUrl\":\"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2023\/02\/inmotion-hosting-logo-yoast.jpg\",\"width\":696,\"height\":696,\"caption\":\"InMotion Hosting\"},\"image\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/inmotionhosting\/\",\"https:\/\/x.com\/InMotionHosting\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/person\/0736f70b4077032374f89709cdc255b7\",\"name\":\"Derrell\",\"sameAs\":[\"https:\/\/www.linkedin.com\/in\/derrell-willis\"],\"url\":\"https:\/\/www.inmotionhosting.com\/support\/author\/derrellw\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Mastering Laravel Pagination | InMotion Hosting","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/mastering-laravel-pagination\/","og_locale":"en_US","og_type":"article","og_title":"Mastering Laravel Pagination | InMotion Hosting","og_description":"Pagination is a critical feature for web applications, enhancing user experience by breaking down large data sets into manageable chunks. Laravel simplifies the implementation of pagination through its built-in paginate() method. This article will guide you through the process of setting up and customizing pagination in Laravel, ensuring a smooth and efficient user experience. Setting Read More >","og_url":"https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/mastering-laravel-pagination\/","og_site_name":"InMotion Hosting Support Center","article_publisher":"https:\/\/www.facebook.com\/inmotionhosting\/","article_published_time":"2024-05-22T18:18:15+00:00","article_modified_time":"2024-05-22T18:38:45+00:00","og_image":[{"width":1200,"height":630,"url":"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2024\/05\/mastering-laravel-pagination.png","type":"image\/png"}],"author":"Derrell","twitter_card":"summary_large_image","twitter_creator":"@InMotionHosting","twitter_site":"@InMotionHosting","twitter_misc":{"Written by":"Derrell","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/mastering-laravel-pagination\/#article","isPartOf":{"@id":"https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/mastering-laravel-pagination\/"},"author":{"name":"Derrell","@id":"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/person\/0736f70b4077032374f89709cdc255b7"},"headline":"Mastering Laravel Pagination","datePublished":"2024-05-22T18:18:15+00:00","dateModified":"2024-05-22T18:38:45+00:00","mainEntityOfPage":{"@id":"https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/mastering-laravel-pagination\/"},"wordCount":888,"commentCount":0,"publisher":{"@id":"https:\/\/www.inmotionhosting.com\/support\/#organization"},"image":{"@id":"https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/mastering-laravel-pagination\/#primaryimage"},"thumbnailUrl":"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2024\/05\/mastering-laravel-pagination.png","articleSection":["Laravel"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/mastering-laravel-pagination\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/mastering-laravel-pagination\/","url":"https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/mastering-laravel-pagination\/","name":"Mastering Laravel Pagination | InMotion Hosting","isPartOf":{"@id":"https:\/\/www.inmotionhosting.com\/support\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/mastering-laravel-pagination\/#primaryimage"},"image":{"@id":"https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/mastering-laravel-pagination\/#primaryimage"},"thumbnailUrl":"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2024\/05\/mastering-laravel-pagination.png","datePublished":"2024-05-22T18:18:15+00:00","dateModified":"2024-05-22T18:38:45+00:00","breadcrumb":{"@id":"https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/mastering-laravel-pagination\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/mastering-laravel-pagination\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/mastering-laravel-pagination\/#primaryimage","url":"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2024\/05\/mastering-laravel-pagination.png","contentUrl":"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2024\/05\/mastering-laravel-pagination.png","width":1200,"height":630},{"@type":"BreadcrumbList","@id":"https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/mastering-laravel-pagination\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.inmotionhosting.com\/support\/"},{"@type":"ListItem","position":2,"name":"Mastering Laravel Pagination"}]},{"@type":"WebSite","@id":"https:\/\/www.inmotionhosting.com\/support\/#website","url":"https:\/\/www.inmotionhosting.com\/support\/","name":"InMotion Hosting Support Center","description":"Web Hosting Support &amp; Tutorials","publisher":{"@id":"https:\/\/www.inmotionhosting.com\/support\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.inmotionhosting.com\/support\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.inmotionhosting.com\/support\/#organization","name":"InMotion Hosting","url":"https:\/\/www.inmotionhosting.com\/support\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/logo\/image\/","url":"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2023\/02\/inmotion-hosting-logo-yoast.jpg","contentUrl":"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2023\/02\/inmotion-hosting-logo-yoast.jpg","width":696,"height":696,"caption":"InMotion Hosting"},"image":{"@id":"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/inmotionhosting\/","https:\/\/x.com\/InMotionHosting"]},{"@type":"Person","@id":"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/person\/0736f70b4077032374f89709cdc255b7","name":"Derrell","sameAs":["https:\/\/www.linkedin.com\/in\/derrell-willis"],"url":"https:\/\/www.inmotionhosting.com\/support\/author\/derrellw\/"}]}},"jetpack_featured_media_url":"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2024\/05\/mastering-laravel-pagination.png","jetpack_sharing_enabled":true,"primary_category":{"id":4486,"name":"Laravel","slug":"laravel","link":"https:\/\/www.inmotionhosting.com\/support\/edu\/laravel\/"},"_links":{"self":[{"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/posts\/127927","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/users\/57032"}],"replies":[{"embeddable":true,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/comments?post=127927"}],"version-history":[{"count":8,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/posts\/127927\/revisions"}],"predecessor-version":[{"id":127953,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/posts\/127927\/revisions\/127953"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/media\/127943"}],"wp:attachment":[{"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/media?parent=127927"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/categories?post=127927"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/tags?post=127927"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}