{"id":3460,"date":"2020-07-11T03:02:29","date_gmt":"2020-07-11T03:02:29","guid":{"rendered":"https:\/\/programmingfields.com\/?p=3460"},"modified":"2026-05-02T06:31:29","modified_gmt":"2026-05-02T06:31:29","slug":"laravel-7-yajra-datatable-with-server-side-processing","status":"publish","type":"post","link":"https:\/\/programmingfields.com\/laravel-7-yajra-datatable-with-server-side-processing\/","title":{"rendered":"Laravel 7 Yajra DataTable with Server Side Processing"},"content":{"rendered":"\n<p>Laravel datatable is a package to optimize the data by providing the features of sorting, paging, searching, etc. If you have thousands or millions of row containing the data then really it will be a slow process to load all the data. So, why don&#8217;t we try to load data in small chunks? This will optimize the view for rendering the data in a cleaved way. It will enhance the page loading speed by retrieving data by breaking down. So, this is possible by using Ajax. Ajax will load data asynchronously without refreshing the page. Therefore it will hit the request to the server and to manage this request, we will have to enable the server-side in the Laravel Datatable. So, let&#8217;s start by creating a new project in Laravel 7.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-light-blue ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/programmingfields.com\/laravel-7-yajra-datatable-with-server-side-processing\/#Prerequisites\" >Prerequisites<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/programmingfields.com\/laravel-7-yajra-datatable-with-server-side-processing\/#Laravel_DataTable_Server_Side\" >Laravel DataTable Server Side<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/programmingfields.com\/laravel-7-yajra-datatable-with-server-side-processing\/#Install_Yajra_DataTable_in_Laravel_7\" >Install Yajra DataTable in Laravel 7<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/programmingfields.com\/laravel-7-yajra-datatable-with-server-side-processing\/#Create_and_Configure_Database_in_Laravel_7\" >Create and Configure Database in Laravel 7<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/programmingfields.com\/laravel-7-yajra-datatable-with-server-side-processing\/#Create_a_Model_and_Migration\" >Create a Model and Migration<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/programmingfields.com\/laravel-7-yajra-datatable-with-server-side-processing\/#Migrate_Database\" >Migrate Database<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/programmingfields.com\/laravel-7-yajra-datatable-with-server-side-processing\/#Add_Mass_Assignment_in_Model\" >Add Mass Assignment in Model<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/programmingfields.com\/laravel-7-yajra-datatable-with-server-side-processing\/#Laravel_Factory_For_Dummy_Records\" >Laravel Factory For Dummy Records<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/programmingfields.com\/laravel-7-yajra-datatable-with-server-side-processing\/#Create_Dummy_Records_Using_Tinker\" >Create Dummy Records Using Tinker<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/programmingfields.com\/laravel-7-yajra-datatable-with-server-side-processing\/#Create_a_Controller_to_Implement_Datatable_Server_Side\" >Create a Controller to Implement Datatable Server Side<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/programmingfields.com\/laravel-7-yajra-datatable-with-server-side-processing\/#Create_a_Route_in_Laravel_7\" >Create a Route in Laravel 7<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/programmingfields.com\/laravel-7-yajra-datatable-with-server-side-processing\/#Create_a_View_For_Yajra_Datatable_Server_Side\" >Create a View For Yajra Datatable Server Side<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/programmingfields.com\/laravel-7-yajra-datatable-with-server-side-processing\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Prerequisites\"><\/span><strong>Prerequisites<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>For creating a Laravel 7 project, you will have the following tools with the required version.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>PHP &gt;= 7.2.5<\/strong><\/li><li><strong>MySQL &gt; 5<\/strong><\/li><li><strong>Apache\/Nginx Server<\/strong><\/li><li><strong>Composer<\/strong><\/li><\/ul>\n\n\n\n<p>So, once you are ready, let&#8217;s create the project.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Laravel_DataTable_Server_Side\"><\/span><strong>Laravel DataTable Server Side<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Open the terminal or command prompt and then create a Laravel 7 project.<\/p>\n\n\n\n<pre title=\"create-project\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">composer create-project --prefer-dist laravel\/laravel yajra-datatable<\/code><\/pre>\n\n\n\n<p>The above command will start creating a new folder. Inside the folder, it will install the Laravel with the required packages.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"865\" height=\"610\" src=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2020\/07\/1.-create-new-project.png?resize=865%2C610&#038;ssl=1\" alt=\"Create Laravel 7 Project\" class=\"wp-image-3465\" srcset=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2020\/07\/1.-create-new-project.png?w=865&amp;ssl=1 865w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2020\/07\/1.-create-new-project.png?resize=300%2C212&amp;ssl=1 300w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2020\/07\/1.-create-new-project.png?resize=768%2C542&amp;ssl=1 768w\" sizes=\"auto, (max-width: 865px) 100vw, 865px\" \/><figcaption><strong>Create Project in Laravel 7<\/strong><\/figcaption><\/figure><\/div>\n\n\n\n<p class=\"has-text-align-center\"><a href=\"https:\/\/programmingfields.com\/send-email-using-gmail-smtp-in-laravel-7\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to Send Email Using Gmail SMTP in Laravel 7<\/a><\/p>\n\n\n\n<p>Once the project has been created, let&#8217;s install the Yajra Datatable package inside the project.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Install_Yajra_DataTable_in_Laravel_7\"><\/span><strong>Install Yajra DataTable in Laravel 7<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Now, we will be installing the Yajra Datatable package in our project. <\/p>\n\n\n\n<pre title=\"install-yajra-datatable\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">composer require yajra\/laravel-datatables-oracle<\/code><\/pre>\n\n\n\n<p>It will take a couple of minutes to add the package. So, wait till it finishes.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"868\" height=\"498\" src=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2020\/07\/2.-Install-yajra-datatable.png?resize=868%2C498&#038;ssl=1\" alt=\"Install datatable package\" class=\"wp-image-3470\" srcset=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2020\/07\/2.-Install-yajra-datatable.png?w=868&amp;ssl=1 868w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2020\/07\/2.-Install-yajra-datatable.png?resize=300%2C172&amp;ssl=1 300w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2020\/07\/2.-Install-yajra-datatable.png?resize=768%2C441&amp;ssl=1 768w\" sizes=\"auto, (max-width: 868px) 100vw, 868px\" \/><figcaption><strong>Install Yajra DataTable in Laravel 7<\/strong><\/figcaption><\/figure><\/div>\n\n\n\n<p>After adding the package, you can verify the installation in the <strong>composer.json<\/strong> file. You can see the Yajra datatable package has been added.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"698\" height=\"596\" src=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2020\/07\/3.-composer-json.png?resize=698%2C596&#038;ssl=1\" alt=\"composer.json in laravel 7\" class=\"wp-image-3473\" srcset=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2020\/07\/3.-composer-json.png?w=698&amp;ssl=1 698w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2020\/07\/3.-composer-json.png?resize=300%2C256&amp;ssl=1 300w\" sizes=\"auto, (max-width: 698px) 100vw, 698px\" \/><figcaption><strong>composer.json file in Laravel 7<\/strong><\/figcaption><\/figure><\/div>\n\n\n\n<p class=\"has-text-align-center\"><a href=\"https:\/\/programmingfields.com\/implement-google-chart-inlaravel-7\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to Implement Google Charts in Laravel 7<\/a><\/p>\n\n\n\n<p>Now, we will configure the database for our project.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Create_and_Configure_Database_in_Laravel_7\"><\/span><strong>Create and Configure Database in Laravel 7<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Create a new database in MySQL. Either you can use MySQL command prompt or <strong>phpMyAdmin <\/strong>for creating the database.<\/p>\n\n\n\n<pre title=\"create-database\" class=\"wp-block-code\"><code lang=\"sql\" class=\"language-sql\">CREATE DATABASE laravel_datatable;<\/code><\/pre>\n\n\n\n<p>After creating the database, we will configure our Laravel 7 project to sync with the database.<\/p>\n\n\n\n<p>Laravel contains the environment file for such type of configuration. This file is present in root of the project with the name .env (environment). So, open the file and add the database credentials there that is showing below. <\/p>\n\n\n\n<pre title=\".env file\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">DB_CONNECTION=mysql\nDB_HOST=127.0.0.1\nDB_PORT=3306\nDB_DATABASE=laravel_datatable\nDB_USERNAME={{-- DATABASE USERNAME -- }}\nDB_PASSWORD={{-- DATABASE PASSWORD -- }}<\/code><\/pre>\n\n\n\n<p>Now, you are ready to go with the database. <\/p>\n\n\n\n<p>In the next step, we will require a Model and migration for the tables. We will be managing the data dynamically from the database. So, let&#8217;s move on.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Create_a_Model_and_Migration\"><\/span><strong>Create a Model and Migration<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Laravel has a default model for the user. So, we can use this model or you can create a new one using the artisan command. In the project directory, we can find the model inside the app folder. The model name is<strong> User.php<\/strong>. The user migration file will be inside the<strong> database\/migrations<\/strong>. <\/p>\n\n\n\n<p>We will be adding some additional fields in the <strong>user migration <\/strong>file.<\/p>\n\n\n\n<pre title=\"create_users_table.php\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">&lt;?php\n\nuse Illuminate\\Database\\Migrations\\Migration;\nuse Illuminate\\Database\\Schema\\Blueprint;\nuse Illuminate\\Support\\Facades\\Schema;\n\nclass CreateUsersTable extends Migration\n{\n    \/**\n     * Run the migrations.\n     *\n     * @return void\n     *\/\n    public function up()\n    {\n        Schema::create('users', function (Blueprint $table) {\n            $table-&gt;id();\n            $table-&gt;string('name');\n            $table-&gt;string('email')-&gt;unique();\n            $table-&gt;string('phone')-&gt;nullable();\n            $table-&gt;string('address')-&gt;nullable();\n            $table-&gt;timestamp('email_verified_at')-&gt;nullable();\n            $table-&gt;string('password');\n            $table-&gt;rememberToken();\n            $table-&gt;timestamps();\n        });\n    }\n\n    \/**\n     * Reverse the migrations.\n     *\n     * @return void\n     *\/\n    public function down()\n    {\n        Schema::dropIfExists('users');\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>After adding the fields, we will migrate the tables. So, that the tables will created in the database with the specified fields.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Migrate_Database\"><\/span>Migrate Database<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>For migrating the tables in the database, we will be using the below command.<\/p>\n\n\n\n<pre title=\"migrate-tables\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">php artisan migrate<\/code><\/pre>\n\n\n\n<p>The above command will generate the tables inside the database. So, now, we are ready with a database and table. Now, let&#8217;s proceed to implement the Yajra Datatable server side. Before implementing the datatable server side, we will require to have the data on which we will apply the Yajra datatable server side processing.<\/p>\n\n\n\n<p class=\"has-text-align-center\"><a href=\"https:\/\/programmingfields.com\/laravel-guzzle-http-client-for-http-request-handling\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to Use Http Client For Request Handling in Laravel 7<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Add_Mass_Assignment_in_Model\"><\/span><strong>Add Mass Assignment in Model<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>What fields of the users table will be managed by the User model need to be specify. As we know, model syncs with the database for the sending and retrieving values. So, add the fillable data there as showing below. <\/p>\n\n\n\n<pre title=\"User.php\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">&lt;?php\n\nnamespace App;\n\nuse Illuminate\\Contracts\\Auth\\MustVerifyEmail;\nuse Illuminate\\Foundation\\Auth\\User as Authenticatable;\nuse Illuminate\\Notifications\\Notifiable;\n\nclass User extends Authenticatable\n{\n    use Notifiable;\n\n    \/**\n     * The attributes that are mass assignable.\n     *\n     * @var array\n     *\/\n    protected $fillable = [\n        'name', 'email', 'password', 'phone', 'address'\n    ];\n\n    \/**\n     * The attributes that should be hidden for arrays.\n     *\n     * @var array\n     *\/\n    protected $hidden = [\n        'password', 'remember_token',\n    ];\n\n    \/**\n     * The attributes that should be cast to native types.\n     *\n     * @var array\n     *\/\n    protected $casts = [\n        'email_verified_at' =&gt; 'datetime',\n    ];\n}\n<\/code><\/pre>\n\n\n\n<p>So, let&#8217;s create some dummy records for the users table. For creating the dummy records, we will be using the Laravel tinker factory class. This will generate the specified number of dummy data in each fields.<\/p>\n\n\n\n<p class=\"has-text-align-center\"><a href=\"https:\/\/programmingfields.com\/laravel-7-upload-multiple-images\/\" target=\"_blank\" rel=\"noreferrer noopener\">Laravel 7 Upload Multiple Images with Image Validation<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Laravel_Factory_For_Dummy_Records\"><\/span><strong>Laravel Factory For Dummy Records<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In the Laravel, factories contain the fields for which we are going to generate the dummy data. Default, there is one factory file for the user as <strong>UserFactory.php<\/strong> file.<\/p>\n\n\n\n<pre title=\"UserFactory.php\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">&lt;?php\n\n\/** @var \\Illuminate\\Database\\Eloquent\\Factory $factory *\/\n\nuse App\\User;\nuse Faker\\Generator as Faker;\nuse Illuminate\\Support\\Str;\n\n\/*\n|--------------------------------------------------------------------------\n| Model Factories\n|--------------------------------------------------------------------------\n|\n| This directory should contain each of the model factory definitions for\n| your application. Factories provide a convenient way to generate new\n| model instances for testing \/ seeding your application's database.\n|\n*\/\n\n$factory-&gt;define(User::class, function (Faker $faker) {\n    return [\n        'name' =&gt; $faker-&gt;name,\n        'email' =&gt; $faker-&gt;unique()-&gt;safeEmail,\n        'phone' =&gt; $faker-&gt;phoneNumber,\n        'address'   =&gt; $faker-&gt;address,\n        'email_verified_at' =&gt; now(),\n        'password' =&gt; '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC\/.og\/at2.uheWG\/igi',\n        'remember_token' =&gt; Str::random(10),\n    ];\n});\n<\/code><\/pre>\n\n\n\n<p>After adding the fields inside the <strong>UserFactory<\/strong> let&#8217;s generate the records using the tinker class.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Create_Dummy_Records_Using_Tinker\"><\/span><strong>Create Dummy Records Using Tinker<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Laravel provides the Tinker class for generating the number of specified records. Basically, this is connected with the Factory class. Factory class associated with the Model and the Model synced with the table inside the database.<\/p>\n\n\n\n<pre title=\"create-records-using-tinker\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">php artisan tinker\nfactory (App\\User::class, 200)-&gt;create()<\/code><\/pre>\n\n\n\n<p>It will take a couple of minutes to generate the dummy records.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"861\" height=\"689\" src=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2020\/07\/4.tinker-factory.png?resize=861%2C689&#038;ssl=1\" alt=\"Laravel 7 Tinker Class for generating dummy records\" class=\"wp-image-3482\" srcset=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2020\/07\/4.tinker-factory.png?w=861&amp;ssl=1 861w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2020\/07\/4.tinker-factory.png?resize=300%2C240&amp;ssl=1 300w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2020\/07\/4.tinker-factory.png?resize=768%2C615&amp;ssl=1 768w\" sizes=\"auto, (max-width: 861px) 100vw, 861px\" \/><figcaption><strong>Laravel Tinker Factory &#8211; Created Dummy Records<\/strong><\/figcaption><\/figure><\/div>\n\n\n\n<p class=\"has-text-align-center\"><a href=\"https:\/\/programmingfields.com\/create-a-crud-application-in-laravel-7\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to Create a CRUD Application in Laravel 7<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Create_a_Controller_to_Implement_Datatable_Server_Side\"><\/span><strong>Create a Controller to Implement Datatable<\/strong> Server Side<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In the next step, we will be implementing the Laravel yajra datatable server side for rendering the data in chunks. So, create a controller for the Users. Then we will add the functionality.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"865\" height=\"59\" src=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2020\/07\/5.-Make-controller.png?resize=865%2C59&#038;ssl=1\" alt=\"Create a controller for user\" class=\"wp-image-3489\" srcset=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2020\/07\/5.-Make-controller.png?w=865&amp;ssl=1 865w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2020\/07\/5.-Make-controller.png?resize=300%2C20&amp;ssl=1 300w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2020\/07\/5.-Make-controller.png?resize=768%2C52&amp;ssl=1 768w\" sizes=\"auto, (max-width: 865px) 100vw, 865px\" \/><figcaption><strong>Created a Controller for User<\/strong><\/figcaption><\/figure><\/div>\n\n\n\n<p>Now, we have the <strong>UserController.php<\/strong>, therefore, let&#8217;s add the functionality to fetch the records from the table. Then we&#8217;ll implement it into the <a href=\"https:\/\/github.com\/yajra\/laravel-datatables\" target=\"_blank\" rel=\"noreferrer noopener\">datatable<\/a> with the server side. <\/p>\n\n\n\n<pre title=\"UserController.php\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">&lt;?php\n\nnamespace App\\Http\\Controllers;\n\nuse Illuminate\\Http\\Request;\nuse App\\User;\nuse DataTables;\n\nclass UserController extends Controller\n{\n    public function index(Request $request) {\n        if ($request-&gt;ajax()) {\n            $data = User::select('*');\n            return Datatables::of($data)\n                    -&gt;addIndexColumn()\n                    -&gt;addColumn('action', function($row){\n     \n                           $btn = '&lt;a href=\"javascript:void(0)\" class=\"edit btn btn-primary btn-sm\"&gt;View&lt;\/a&gt;';\n       \n                            return $btn;\n                    })\n                    -&gt;rawColumns(['action'])\n                    -&gt;make(true);\n        }\n        \n        return view('users-data');\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>For the above function call we will need to create a route. Then using the route we will be calling this function. So, let&#8217;s create a route.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Create_a_Route_in_Laravel_7\"><\/span><strong>Create a Route in Laravel 7<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>This route will be created inside the web.php. Hence, just add the below route.<\/p>\n\n\n\n<pre title=\"web.php\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">Route::get('users', ['uses'=&gt;'UserController@index', 'as'=&gt;'users.index']);<\/code><\/pre>\n\n\n\n<p>Here, the route has been created. Now, for displaying the result in the datatable format, we will create a view.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Create_a_View_For_Yajra_Datatable_Server_Side\"><\/span><strong>Create a View<\/strong> For Yajra Datatable Server Side<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In Laravel, you can create a view by creating a new blade file. Here, I have created a view with the name <strong>users-data.blade.php<\/strong>.<\/p>\n\n\n\n<p>Inside the view add the below snippet to display the users record in the table format.<\/p>\n\n\n\n<pre title=\"users-data.blade.php\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">&lt;!doctype html&gt;\n&lt;html lang=\"en\"&gt;\n  &lt;head&gt;\n    &lt;title&gt;Laravel 7 - Yajra Datatable Implementation&lt;\/title&gt;\n    &lt;meta charset=\"utf-8\"&gt;\n    &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1, shrink-to-fit=no\"&gt;\n    &lt;meta name=\"csrf-token\" content=\"{{ csrf_token() }}\"&gt;\n\n    &lt;link rel=\"stylesheet\" href=\"https:\/\/stackpath.bootstrapcdn.com\/bootstrap\/4.3.1\/css\/bootstrap.min.css\" integrity=\"sha384-ggOyR0iXCbMQv3Xipma34MD+dH\/1fQ784\/j6cY\/iJTQUOhcWr7x9JvoRxT2MZw1T\" crossorigin=\"anonymous\"&gt;\n    &lt;link href=\"https:\/\/cdn.datatables.net\/1.10.16\/css\/jquery.dataTables.min.css\" rel=\"stylesheet\"&gt;\n    &lt;link href=\"https:\/\/cdn.datatables.net\/1.10.19\/css\/dataTables.bootstrap4.min.css\" rel=\"stylesheet\"&gt;\n  &lt;\/head&gt;\n  &lt;body&gt;\n\n  &lt;div class=\"container mt-5\"&gt;\n    &lt;h3 class=\"text-center font-weight-bold\"&gt;Yajra Datatable Server Side in Laravel 7 &lt;\/h3&gt;\n    &lt;table class=\"table mt-4\" id=\"usersTable\"&gt;\n        &lt;thead&gt;\n            &lt;th&gt; # &lt;\/th&gt;\n            &lt;th&gt; Name &lt;\/th&gt;\n            &lt;th&gt; Email &lt;\/th&gt;\n            &lt;th&gt; Phone &lt;\/th&gt;\n            &lt;th&gt; Action &lt;\/th&gt;\n        &lt;\/thead&gt;\n        &lt;tbody&gt;\n        &lt;\/tbody&gt;\n    &lt;\/table&gt;\n  &lt;\/div&gt;\n\n  &lt;script src=\"https:\/\/code.jquery.com\/jquery-3.5.0.js\" integrity=\"sha256-r\/AaFHrszJtwpe+tHyNi\/XCfMxYpbsRg2Uqn0x3s2zc=\" crossorigin=\"anonymous\"&gt;&lt;\/script&gt;\n  &lt;script src=\"https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/popper.js\/1.14.7\/umd\/popper.min.js\" integrity=\"sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1\" crossorigin=\"anonymous\"&gt;&lt;\/script&gt;\n  &lt;script src=\"https:\/\/stackpath.bootstrapcdn.com\/bootstrap\/4.3.1\/js\/bootstrap.min.js\" integrity=\"sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf\/nJGzIxFDsf4x0xIM+B07jRM\" crossorigin=\"anonymous\"&gt;&lt;\/script&gt;\n  &lt;script src=\"https:\/\/cdn.datatables.net\/1.10.16\/js\/jquery.dataTables.min.js\"&gt;&lt;\/script&gt;\n  &lt;script src=\"https:\/\/cdn.datatables.net\/1.10.19\/js\/dataTables.bootstrap4.min.js\"&gt;&lt;\/script&gt;\n\n    &lt;script type=\"text\/javascript\"&gt;\n        $(document).ready(function() {\n            var table = $('#usersTable').DataTable({\n                processing: true,\n                serverSide: true,\n                ajax: \"{{ route('users.index') }}\",\n                columns: [\n                    {data: 'id', name: 'id'},\n                    {data: 'name', name: 'name'},\n                    {data: 'email', name: 'email'},\n                    {data: 'phone', name: 'phone'},\n                    {data: 'action', name: 'action', orderable: false, searchable: false},\n                ]\n            });\n        });\n      &lt;\/script&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n\n\n\n<p>Now, the application is ready to serve. So, let&#8217;s see the result.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"548\" src=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2020\/07\/yajra-datatable.png?resize=1024%2C548&#038;ssl=1\" alt=\"datatable result\" class=\"wp-image-3495\" srcset=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2020\/07\/yajra-datatable.png?resize=1024%2C548&amp;ssl=1 1024w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2020\/07\/yajra-datatable.png?resize=300%2C161&amp;ssl=1 300w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2020\/07\/yajra-datatable.png?resize=768%2C411&amp;ssl=1 768w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2020\/07\/yajra-datatable.png?w=1366&amp;ssl=1 1366w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><figcaption><strong>Yajra DataTable Result with Server Side<\/strong><\/figcaption><\/figure><\/div>\n\n\n\n<p>In the result, you will see the data has been loaded in chunks of 10 records on every click of pagination. It will make data retrieval fast than the normal load of data into the datatable.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span><strong>Conclusion<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The server side in the Laravel datatable has been implemented in this project. It will load the data very fast from the database. Actually, in every click of the pagination number the data will be loaded in small amount. So, the entire data will not be loaded at once. You can test it for the large number of records. In that case, you will analyze the loading time has been reduced. So, I hope this will be a real example for implementing the datatable server side.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Laravel datatable is a package to optimize the data by providing the features of sorting, paging, searching, etc. If you have thousands or millions of row containing the data then really it will be a slow process to load all the data. So, why don&#8217;t we try to load data in small chunks? This will [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":3500,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[2564],"tags":[1740,1516,1739,1738],"yst_prominent_words":[100,392,1502,1730,1729,900,1737,1736,1724,456,1447,1336,1731,1499,1514,1513,1726,1733,1732],"class_list":{"0":"post-3460","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-laravel","8":"tag-datatable-in-laravel-7","9":"tag-laravel-7-yajra-datatable","10":"tag-server-side-processing","11":"tag-yajra-datatable-server-side-processing","12":"entry"},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2020\/07\/Yajra-Datatable-with-Server-Side.png?fit=2240%2C1260&ssl=1","jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/posts\/3460","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/comments?post=3460"}],"version-history":[{"count":1,"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/posts\/3460\/revisions"}],"predecessor-version":[{"id":8821,"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/posts\/3460\/revisions\/8821"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/media\/3500"}],"wp:attachment":[{"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/media?parent=3460"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/categories?post=3460"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/tags?post=3460"},{"taxonomy":"yst_prominent_words","embeddable":true,"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/yst_prominent_words?post=3460"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}