DataTables Server-side Processing using Laravel

DataTables Server-side Processing using Laravel

In this post we will show you DataTables Server-side Processing using Laravel, hear for DataTables Server-side Processing using Laravel we will give you demo and example for implement.

we will demonstrate to you the most straightforward approach to actualize DataTables jQuery Plugin with remote server side handling in Laravel or in simple word we can say DataTables Server-side Processing using Laravel and jQuery. Here we will demonstrate to you industry standards to bring information from remote MySQL database through ajax in Laravel. For the individuals who don’t here about Datatables, DataTables is a table improving module for the jQuery Javascript library that aides in including arranging, paging and separating capacities to plain HTML tables with insignificant exertion. The primary objective is to upgrade the openness of information in typical HTML tables.

Presently before we begin coding incorporate Datatables CSS record and Javascript documents from CDN in your view page as takes after.

<!-- css and js for DataTables Server-side Processing -->
<link rel="stylesheet" type="text/css" href=""/>

<script type="text/javascript" src=""></script>

Presently we should comprehend what all undertakings we have to do

  • We need to confine the measure of the table. (As a matter of course 10, 25, 50, 100 or 200 entries)
  • The Pagination task.
  • Now Implement look functionality.

All above undertaking will be done in the controller and it will be clarified later in this post DataTables Server-side Processing using Laravel.

Now let’s start coding for DataTables Server-side Processing using Laravel

In the view page code for HTML table is given beneath for DataTables Server-side Processing.

<h2>DataTables Server-side Processing using Laravel - demo </h2>
<div class="row divlcs">
<div class="col-md-12 divlcs>
<table class="table table-bordered divlcs" id="posts_table">
<th>Created Time</th>

DataTables Server-side Processing javascript code is given below.

$(document).ready(function () {
"serverSide": true,
"processing": true,
"url": "{{ url('allposts') }}",
"dataType": "json",
"type": "POST",
"data":{ _token: "{{csrf_token()}}"}
"columns": [
{ "data": "id" },
{ "data": "title" },
{ "data": "body" },
{ "data": "created_at" },
{ "data": "options" }


Note: Do not forget to pass CSRF Token along with ajax POST request as above. Otherwise, internal server error 500 will occur. This is because Laravel checks CSRF token in all POST controller functions by default to ensure maximum protection.

Now the code for post routes in routes/web.php

// set Route
Route::post('allposts', 'PostController@allPosts' )->name('allposts');

Now we using Laravel Eloquent for simplicity and code size reduction. So we need to create a Post model.

DataTables Server-side Processing Post model code is given below.

// namespace
namespace App;

use Illuminate\Database\Eloquent\Model;
// class Post
class Post extends Model


Note: If you don’t have a clue about the essential idea of Laravel Eloquent ORM then all controller code may discover small befuddling to you. If it’s not too much trouble consider discovering that before continuing this post DataTables Server-side Processing using Laravel.

Presently before we begin coding our controller we have to realize that Datatable will post a great deal of information to controller work amid Ajax ask. I found that capacity by utilizing a module in firefox called Firebug. On the off chance that you are a web designer and utilize ajax frequently then I to a great degree prescribe Firebug.

For this post DataTables Server-side Processing using Laravel, we have to take a gander at just 6 post demands.

length: Number of records that the table can show in the present draw. It is normal that the quantity of records returned will be equivalent to this number unless the server has less records to return.

  1. start: Paging initially record pointer. This is the begin point in the present informational collection (0 record based – i.e. 0 is the principal record).
  2. order[0]column: Column to which requesting ought to be connected. This is a file reference to the segments cluster of data that is additionally submitted to the server.
  3. order[0]dir: Ordering heading for this segment. It will be asc or desc to demonstrate rising requesting or diving requesting, respectively.
  4. search[value]: The Global hunt value.
  5. draw: Draw counter. This is utilized by DataTables to guarantee that the Ajax comes back from server-side preparing demands are attracted arrangement by DataTables (Ajax asks for are nonconcurrent and in this manner can return out of sequence).

Presently the code for allPost work in PostController for DataTables Server-side Processing.

public function allPosts(Request $request)
$totalFilteredRecord = $totalDataRecord = $draw_val = "";
$columns_list = array(
0 =>'id',
1 =>'title',
2=> 'body',
3=> 'created_at',
4=> 'id',

$totalDataRecord = Post::count();

$totalFilteredRecord = $totalDataRecord;

$limit_val = $request->input('length');
$start_val = $request->input('start');
$order_val = $columns_list[$request->input('order.0.column')];
$dir_val = $request->input('order.0.dir');

$post_data = Post::offset($start_val)
else {
$search_text = $request->input('search.value');

$post_data =  Post::where('id','LIKE',"%{$search_text}%")
->orWhere('title', 'LIKE',"%{$search_text}%")

$totalFilteredRecord = Post::where('id','LIKE',"%{$search_text}%")
->orWhere('title', 'LIKE',"%{$search_text}%")

$data_val = array();
foreach ($post_data as $post_val)
$datashow =  route('',$post_val->id);
$dataedit =  route('posts_table.edit',$post_val->id);

$postnestedData['id'] = $post_val->id;
$postnestedData['title'] = $post_val->title;
$postnestedData['body'] = substr(strip_tags($post_val->body),0,50).".....";
$postnestedData['created_at'] = date('j M Y h:i a',strtotime($post_val->created_at));
$postnestedData['options'] = "&emsp;<a href='{$datashow}'class='showdata' title='SHOW DATA' ><span class='showdata glyphicon glyphicon-list'></span></a>&emsp;<a href='{$dataedit}' class='editdata' title='EDIT DATA' ><span class='editdata glyphicon glyphicon-edit'></span></a>";
$data_val[] = $postnestedData;

$draw_val = $request->input('draw');
$get_json_data = array(
"draw"            => intval($draw_val),
"recordsTotal"    => intval($totalDataRecord),
"recordsFiltered" => intval($totalFilteredRecord),
"data"            => $data_val

echo json_encode($get_json_data);


DataTables Server-side Processing how about we see the output image::

DataTables Server-side Processing using Laravel example and demo
DataTables Server-side Processing using Laravel example and demo

Note: The section exhibit is utilized to recognize which MySQL database table segment ought to be arranged in climbing or slipping request. They are the genuine names of the database segments. Their tally must be equivalent to Datatables sections tally.

you folks can guarantee that above code will work 100%. I additionally made a post on DataTable Server-side Processing in Codeigniter. In any case, since CodeIgniter take after conventional MVC and does not has bolster for ORM the quantity of codes is higher than Laravel. In the event that anyone needs to know the most effortless approach to actualize datatables with CodeIgniter please remark beneath and on the off chance that anyone has any proposals or questions or need any assistance remark underneath.

Hope this code and post will helped you for implement DataTables Server-side Processing using Laravel. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve onlincode. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

Leave a Comment

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

8  +  2  =  

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US