How to implement DataTables server-side in laravel

In this post, I will be show you the easiest  way to implement  DataTables  server side processing in Laravel. Here I will show you how to fetch data  using ajax in laravel and render that data in DataTable with searching, Pagination and sorting with server side processing.

This post is an extension to a previous post on DataTables, where we initiated data tables with basic initialization. As many readers suggested to make a tutorial on DataTables server-side. . In this post we have explain step by step. Because Jquery DataTables will add some advance features like quick search of table data, it will make automatic pagination without write of code, table column ordering, sorting of table column and many more.

Now before we start coding include Datatables CSS file and Javascript files from CDN in your view page as follows.





Now let’s understand what all tasks we need to do

  1. We need to limit the size of the table. (By default 10,25,50 or 100 entries)
  2. Now Implement search functionality and sort.
  3. The Pagination task.

All above task will be done in the controller and it will be explained later in this tutorial.

Now let’s start coding.

In the view page code for HTML table is given below.

 

Now we need to add javascript code, you can add that code in your view file or any javascript file which is include with this view. Please copy the given code and paste in your file

 

Note:- Don’t forgot to pass CSRF Token along with ajax POST request as above. Otherwise ajax occurs internal server 500, Laravel checks CSRF Token in all post request by default ensure maximum protection.

Now we need to create routes for list data and load view

Now create product model file, Please copy below code and paste

 

Note:  If you don’t know the basic concept of Laravel Eloquent ORM then all controller code may find little confusing to you. Please consider learning that before proceeding this tutorial.



Now create Demo controller file in app/Http/Controllers/DemoController.php

 

 

View Demo
 

I hope you might have understood it properly, keep reading our other blog posts for more coding tricks.

Thanks!!

Add a Comment

Your email address will not be published. Required fields are marked *