{"id":11001,"date":"2022-03-30T13:27:38","date_gmt":"2022-03-30T13:27:38","guid":{"rendered":"https:\/\/ui-lib.com\/blog\/?p=11001"},"modified":"2022-07-13T08:59:13","modified_gmt":"2022-07-13T08:59:13","slug":"laravel-pagination","status":"publish","type":"post","link":"https:\/\/ui-lib.com\/blog\/laravel-pagination\/","title":{"rendered":"Effortless Pagination With Laravel!"},"content":{"rendered":"\n<p id=\"59e0\">Manual pagination is a very tedious job for any developer. But with Laravel, there is no need to write any extra code for paginating your data! At <a href=\"https:\/\/ui-lib.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">UI-Lib<\/a>, we believe Laravel is always there to save our day from writing complex codes. And as always, Laravel has never underestimated us. When it came to data pagination, Laravel took the responsibility to take care of the pagination side. And doing so, helped us focus more on handling our data and effortlessly paginate them. <\/p>\n\n\n\n<p>We all know what pagination is. It basically means separating our contents both in print or in digital form into discreet pages.  But like Laravel we at <a href=\"https:\/\/ui-lib.com\/\">UI-Lib<\/a> try our best to make you understand anything we write, effortlessly. So for the newbies, by saying pagination we mean the thing shown in the image below,<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized is-style-default\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/ui-lib.com\/blog\/wp-content\/uploads\/2022\/03\/Screenshot-9.png\" alt=\"pagination laravel\" class=\"wp-image-11003\" width=\"935\" height=\"80\"\/><\/figure>\n\n\n\n<p>Now let us understand how we can easily paginate our data. For this, we need to perform the following steps<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Creating Project with Laravel<\/li><li>Database Configuration<\/li><li>Defining and Migrating Model<\/li><li>Fake Data Generation<\/li><li>Controller and Route Creation<\/li><li>Create View File<\/li><li>Use Pagination<\/li><li>Run Laravel Application<\/li><\/ol>\n\n\n\n<p>Let&#8217;s dive into the process where we can see how easily we can use pagination in our project with Laravel,<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-creating-project-laravel-pagination-with-laravel\">Creating Project Laravel-Pagination With Laravel<\/h2>\n\n\n\n<p>To practically understand how pagination is made easy with Laravel we first need to create a project with Laravel. Creating a project with Laravel is achieved with just one command, <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ncomposer create-project laravel\/laravel laravel-pagination\n<\/pre><\/div>\n\n\n<p>Now our project folder is created. To work on our project we need to go inside the project directory,<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ncd laravel-pagination\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-database-configuration\">Database Configuration<\/h2>\n\n\n\n<p>Next, we need to configure our <code>.env<\/code> file inside the project folder. We need to start a local server on our local development system. For that, we are using XAMPP. Now we need to add the database configuration in our <code>.env<\/code> file to establish a connection between Laravel and MySQL. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nDB_CONNECTION=mysql\nDB_HOST=localhost\nDB_PORT=3306\nDB_DATABASE=laravel_db\nDB_USERNAME=root\nDB_PASSWORD=password\n<\/pre><\/div>\n\n\n<p>As a reminder, you need to create a database named &#8220;laravel_db&#8221; to work with the above code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-defining-and-migrating-model\">Defining and Migrating Model<\/h2>\n\n\n\n<p>We are at the stage now where we should start building our model. A model basically means the logical structure of a table in our database. By the following command, Laravel will automatically generate a Model named Student.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nphp artisan make:model Student -m\n<\/pre><\/div>\n\n\n<p><code>-m<\/code> means Laravel will also create a migration file where we can define the columns of the Student table. In the <code>database\/migrations\/timestamp_create_students_table.php<\/code> file let us define the following columns in the <code>up<\/code> method,<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\npublic function up()\n{\n    Schema::create(&#039;students&#039;, function (Blueprint $table) {\n        $table-&gt;id();\n        $table-&gt;string(&#039;firstname&#039;);\n        $table-&gt;string(&#039;lastname&#039;);\n        $table-&gt;string(&#039;email&#039;)-&gt;unique();\n    });\n}\n<\/pre><\/div>\n\n\n<p>Now in the <code>app\/Models\/Student.php<\/code> file, we should add the fillable array so that we can add values to the columns.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nnamespace App\\Models;\nuse Illuminate\\Database\\Eloquent\\Factories\\HasFactory;\nuse Illuminate\\Database\\Eloquent\\Model;\nclass Student extends Model\n{\n    use HasFactory;\n    protected $fillable = &#x5B;\n        &#039;firstname&#039;,\n        &#039;lastname&#039;,\n        &#039;email&#039;,\n    ];\n}\n<\/pre><\/div>\n\n\n<p>Now we are ready to migrate,<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nphp artisan migrate\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-fake-data-generation-for-laravel-pagination\">Fake Data Generation For Laravel Pagination<\/h2>\n\n\n\n<p>Pagination is a process where we can display a large amount of data in groups. For that, we need to generate a load of data.  Faker a built-in package provided by Laravel can come in handy now. Faker helps to create a specific amount of fake data to start working on our project. <\/p>\n\n\n\n<p>In the <code>database\/seed\/DatabaseSeeder.php<\/code> file let us write the following code,<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nuse Illuminate\\Database\\Seeder;\nuse Illuminate\\Support\\Facades\\DB;\nuse Faker\\Factory as Faker;\nclass DatabaseSeeder extends Seeder\n{\n    \/**\n     * Seed the application&#039;s database.\n     *\n     * @return void\n     *\/\n    public function run()\n    {\n        $faker = Faker::create();\n    \tforeach (range(1,1000) as $index) {\n            DB::table(&#039;students&#039;)-&gt;insert(&#x5B;\n                &#039;firstname&#039; =&gt; $faker-&gt;firstname,\n                &#039;lastname&#039; =&gt; $faker-&gt;lastname,\n                &#039;email&#039; =&gt; $faker-&gt;email,\n            ]);\n        }\n    }\n}\n<\/pre><\/div>\n\n\n<p>The above code will generate 1000 fake data in the Student model. For that, we need to run the command below,<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nphp artisan db:seed\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-controller-and-route-creation\">Controller and Route Creation<\/h2>\n\n\n\n<p>We are ready to create our controller file by the following command,<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nphp artisan make:controller StudentsController\n<\/pre><\/div>\n\n\n<p>Now we should  start building the logic inside the controller file we created,<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nnamespace App\\Http\\Controllers;\nuse Illuminate\\Http\\Request;\nuse App\\Models\\Student;\nclass StudentsController extends Controller\n{\n    public function getStudentData(){\n      $students = Student::all();\n      return view(&#039;home&#039;, compact(&#039;students&#039;));\n    }\n}\n<\/pre><\/div>\n\n\n<p>Lastly, we define the route that will help us render the view file. In the <code>routes\/web.php<\/code> file,<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nuse App\\Http\\Controllers\\ StudentsController;\n\nRoute::get(&#039;\/&#039;, &#x5B;StudentsController::class, &#039;getStudentData&#039;]);\n<\/pre><\/div>\n\n\n<p>The above code will display the <code>home.blade.php<\/code> file in the browser.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-create-view-file-to-display-laravel-pagination\">Create View File To Display Laravel Pagination<\/h2>\n\n\n\n<p>We now create a <code>home.blade.php<\/code> file and show the data from our Student model.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;!DOCTYPE html&gt;\n    &amp;lt;html&gt;\n    &amp;lt;head&gt;\n        &amp;lt;title&gt;Laravel Pagination&amp;lt;\/title&gt;\n    &amp;lt;\/head&gt;\n    &amp;lt;body&gt;\n        &amp;lt;div&gt;\n            &amp;lt;table&gt;\n                &amp;lt;thead&gt;\n                    &amp;lt;tr&gt;\n                        &amp;lt;th&quot;&gt;#&amp;lt;\/th&gt;\n                        &amp;lt;th&gt;First name&amp;lt;\/th&gt;\n                        &amp;lt;th&gt;Last name&amp;lt;\/th&gt;\n                        &amp;lt;th&gt;Email&amp;lt;\/th&gt;\n                    &amp;lt;\/tr&gt;\n                &amp;lt;\/thead&gt;\n                &amp;lt;tbody&gt;\n                    @foreach($students as $student)\n                    &amp;lt;tr&gt;\n                        &amp;lt;th&gt;{{ $student-&gt;id }}&amp;lt;\/th&gt;\n                        &amp;lt;td&gt;{{ $student-&gt;firstname }}&amp;lt;\/td&gt;\n                        &amp;lt;td&gt;{{ $student-&gt;lastname }}&amp;lt;\/td&gt;\n                        &amp;lt;td&gt;{{ $student-&gt;email }}&amp;lt;\/td&gt;\n                    &amp;lt;\/tr&gt;\n                    @endforeach\n                &amp;lt;\/tbody&gt;\n            &amp;lt;\/table&gt;\n        &amp;lt;\/div&gt;\n    &amp;lt;\/body&gt;\n&amp;lt;\/html&gt;\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-use-laravel-pagination\"> Use Laravel Pagination <\/h2>\n\n\n\n<p>In this step, the interesting and the most effortless thing begins. Previously for creating pagination we needed to manually write a code for paginating data. Now you will be amazed to know that we just need to do a simple thing for pagination. In the <code>StudentsController.php<\/code> file where we defined <code>all()<\/code> method we just need to change that and call the <code>paginate()<\/code> method. <code>paginate()<\/code> takes a simple integer value which will indicate how much data to show on a single page.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nnamespace App\\Http\\Controllers;\nuse Illuminate\\Http\\Request;\nuse App\\Models\\Student;\nclass StudentsController extends Controller\n{\n    public function getStudentData(){\n      $students = Student::paginate(25);\n      return view(&#039;home&#039;, compact(&#039;students&#039;));\n    }\n}\n<\/pre><\/div>\n\n\n<p>The above code will show the first 25 student&#8217;s data in the view file and add a pagination bar that will link the other student&#8217;s data. But to do that in the <code>home.blade.php<\/code> we should add the following code,<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n {!! $students-&gt;links() !!}\n<\/pre><\/div>\n\n\n<p>The entire <code>home.blade.php<\/code> the file will look like below,<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;!DOCTYPE html&gt;\n    &amp;lt;html&gt;\n    &amp;lt;head&gt;\n        &amp;lt;title&gt;Laravel Pagination&amp;lt;\/title&gt;\n    &amp;lt;\/head&gt;\n    &amp;lt;body&gt;\n        &amp;lt;div&gt;\n            &amp;lt;table&gt;\n                &amp;lt;thead&gt;\n                    &amp;lt;tr&gt;\n                        &amp;lt;th&quot;&gt;#&amp;lt;\/th&gt;\n                        &amp;lt;th&gt;First name&amp;lt;\/th&gt;\n                        &amp;lt;th&gt;Last name&amp;lt;\/th&gt;\n                        &amp;lt;th&gt;Email&amp;lt;\/th&gt;\n                    &amp;lt;\/tr&gt;\n                &amp;lt;\/thead&gt;\n                &amp;lt;tbody&gt;\n                    @foreach($students as $student)\n                    &amp;lt;tr&gt;\n                        &amp;lt;th&gt;{{ $student-&gt;id }}&amp;lt;\/th&gt;\n                        &amp;lt;td&gt;{{ $student-&gt;firstname }}&amp;lt;\/td&gt;\n                        &amp;lt;td&gt;{{ $student-&gt;lastname }}&amp;lt;\/td&gt;\n                        &amp;lt;td&gt;{{ $student-&gt;email }}&amp;lt;\/td&gt;\n                    &amp;lt;\/tr&gt;\n                    @endforeach\n                &amp;lt;\/tbody&gt;\n            &amp;lt;\/table&gt;\n          {!! $students-&gt;links() !!}\n        &amp;lt;\/div&gt;\n    &amp;lt;\/body&gt;\n&amp;lt;\/html&gt;\n<\/pre><\/div>\n\n\n<p>Thus, just like that, we can add pagination to our Laravel project.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-run-laravel-application\">Run Laravel Application<\/h2>\n\n\n\n<p>Finally, to see what we created we should run the Laravel application with the following command and check the result on the URL <a href=\"http:\/\/127.0.0.1:8000\/\">http:\/\/127.0.0.1:8000<\/a>,<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nphp artisan serve\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-conclusion\">Conclusion<\/h2>\n\n\n\n<p>Laravel, for us, is an amazing web framework. For this reason, we are always eager to explore what features we can achieve from it. As a result, we stumble upon many exciting functionalities. And, that is why we can bring you new blogs and products every day.<\/p>\n\n\n\n<p>Pagination was a nightmare for us previously, running a loop for a certain number of times and then decorating the pagination style. However, Laravel thought about us and tried its best to solve this issue. And, we can say with certainty, that it has completely made pagination effortless for us.    <\/p>\n\n\n\n<h2 class=\"has-text-align-center wp-block-heading\"><span class=\"has-inline-color has-luminous-vivid-orange-color\">Our Products Based on Laravel<\/span><\/h2>\n\n\n\n<figure class=\"wp-block-embed aligncenter is-type-wp-embed is-provider-ui-lib wp-block-embed-ui-lib\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"2ei7YPGuum\"><a href=\"https:\/\/ui-lib.com\/downloads\/aatrox-tailwindcss-admin-dashboard-template\/\">Aatrox &#8211; Tailwind HTML + Laravel Dashboard<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;Aatrox &#8211; Tailwind HTML + Laravel Dashboard&#8221; &#8212; Ui Lib\" src=\"https:\/\/ui-lib.com\/downloads\/aatrox-tailwindcss-admin-dashboard-template\/embed\/#?secret=Mv8RxnlEs8#?secret=2ei7YPGuum\" data-secret=\"2ei7YPGuum\" width=\"500\" height=\"282\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">2. <a href=\"https:\/\/1.envato.market\/ZdeBzz\" target=\"_blank\" rel=\"noopener\">WorkTick &#8211; Ultimate HRM &amp; Project Management Software<\/a><\/h3>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. <a href=\"https:\/\/1.envato.market\/EaAgR9\" target=\"_blank\" rel=\"noopener\">Stocky &#8211; Ultimate Inventory Management System with Pos<\/a><\/h3>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4. <a href=\"https:\/\/1.envato.market\/MeKGo\" target=\"_blank\" rel=\"noopener\">Gull &#8211; Bootstrap &amp; Laravel Admin Dashboard Template<\/a><\/h3>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"has-text-align-center wp-block-heading\" id=\"83d3\"><span class=\"has-inline-color has-luminous-vivid-orange-color\">Relevant Articles<\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/ui-lib.com\/blog\/best-laravel-admin-templates\/\" rel=\"noreferrer noopener\" target=\"_blank\">Best Laravel Admin Dashboard Templates<\/a><\/li><li><a href=\"https:\/\/uilib.hashnode.dev\/tired-of-huge-and-complex-coding-for-permission-handling\" rel=\"noreferrer noopener\" target=\"_blank\">Tired Of Huge And Complex Coding For Permission Handling?<\/a><\/li><li><a href=\"https:\/\/uilib.hashnode.dev\/manage-your-large-laravel-application-with-simplicity\" rel=\"noreferrer noopener\" target=\"_blank\">Manage Your Large Laravel Application With Simplicity<\/a><\/li><li><a href=\"https:\/\/uilib.hashnode.dev\/need-authentication-features-for-your-project-built-with-laravel\" rel=\"noreferrer noopener\" target=\"_blank\">Need Authentication Features For Your Project Built With Laravel?<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Manual pagination is a very tedious job for any developer. But with Laravel, there is no need to write any extra code for paginating your&#8230;<\/p>\n<div class=\"more-link-wrapper\"><a class=\"more-link\" href=\"https:\/\/ui-lib.com\/blog\/laravel-pagination\/\">Continue reading<span class=\"screen-reader-text\">Effortless Pagination With Laravel!<\/span><\/a><\/div>\n","protected":false},"author":5,"featured_media":11013,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[868],"tags":[594,598,597,600,593,589,599,590,596,591,601,592,595],"class_list":["post-11001","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-laravel","tag-auto-pagionate","tag-create-laravel-pagination","tag-create-pagination","tag-easy-pagination","tag-effortless-pagination","tag-laravel","tag-laravel-paginatiopn-creation","tag-laravel-pagination","tag-paginate-data","tag-pagination","tag-pagination-development-with-laravel","tag-pagination-laravel","tag-pagination-with-laravel","entry"],"_links":{"self":[{"href":"https:\/\/ui-lib.com\/blog\/wp-json\/wp\/v2\/posts\/11001","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ui-lib.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ui-lib.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ui-lib.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ui-lib.com\/blog\/wp-json\/wp\/v2\/comments?post=11001"}],"version-history":[{"count":11,"href":"https:\/\/ui-lib.com\/blog\/wp-json\/wp\/v2\/posts\/11001\/revisions"}],"predecessor-version":[{"id":12101,"href":"https:\/\/ui-lib.com\/blog\/wp-json\/wp\/v2\/posts\/11001\/revisions\/12101"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ui-lib.com\/blog\/wp-json\/wp\/v2\/media\/11013"}],"wp:attachment":[{"href":"https:\/\/ui-lib.com\/blog\/wp-json\/wp\/v2\/media?parent=11001"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ui-lib.com\/blog\/wp-json\/wp\/v2\/categories?post=11001"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ui-lib.com\/blog\/wp-json\/wp\/v2\/tags?post=11001"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}