Server side Laravel DataTables example

Today, We want to share with you Server side Laravel DataTables example.In this post we will show you DataTables Server-side Processing with Laravel 5.8, hear for Laravel 5.8 – DataTables Server-side Processing we will give you demo and example for implement.In this post, we will learn about Laravel 5.8 Ajax Crud Tutorial using DataTables with an example.

Server side Laravel DataTables example

There are the Following The simple About Server side Laravel DataTables example Full Information With Example and source code.

As I will cover this Post with live Working example to develop How to implement DataTables server-side in laravel, so the datatables jquery plugin with php mysql and bootstrap for this example is following below.

Create Laravel 5.8 View

resources/views/product.blade.php

<!DOCTYPE html>
<html>
<head>
<title>Laravel 5.8 - DataTables Server-side Processing</title>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/dt-1.10.12/datatables.min.css"/>
</head>
<body>
 <div class="row">
    <div class="col-md-12">
               <table class="table table-bordered" id="products">
                    <thead>
                           <th>Id</th>
                           <th>Title</th>
                           <th>Body</th>
                           <th>Created At</th>
                           <th>Options</th>
                    </thead>                
               </table>
        </div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/v/dt/dt-1.10.12/datatables.min.js"></script>
<script>
    $(document).ready(function () {
        $('#products').DataTable({
            "processing": true,
            "serverSide": true,
            "ajax":{
                     "url": "{{ url('allproducts') }}",
                     "dataType": "json",
                     "type": "POST",
                     "data":{ _token: "{{csrf_token()}}"}
                   },
            "columns": [
                { "data": "id" },
                { "data": "title" },
                { "data": "body" },
                { "data": "created_at" },
                { "data": "options" }
            ]    

        });
    });
</script>
</body>
</html>

Create Laravel Model, controller and route

Create Laravel Route

Route::post('allproducts', '[email protected]' )->name('allproducts');

The Product model code is given below.

htdocs/app_name/app/Models/Product.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    
}

Now the code for allproduct function in productController.

Create Laravel Controller

productController.php

htdocs/app_name/app/Http/Controllers/productController.php

public function allproducts(Request $request)
    {
        
        $columns = array( 
                            0 =>'id', 
                            1 =>'title',
                            2=> 'body',
                            3=> 'created_at',
                            4=> 'id',
                        );
  
        $totalData = Product::count();
            
        $totalFiltered = $totalData; 

        $limit = $request->input('length');
        $start = $request->input('start');
        $order = $columns[$request->input('order.0.column')];
        $dir = $request->input('order.0.dir');
            
        if(empty($request->input('search.value')))
        {            
            $products = Product::offset($start)
                         ->limit($limit)
                         ->orderBy($order,$dir)
                         ->get();
        }
        else {
            $search = $request->input('search.value'); 

            $products =  Product::where('id','LIKE',"%{$search}%")
                            ->orWhere('title', 'LIKE',"%{$search}%")
                            ->offset($start)
                            ->limit($limit)
                            ->orderBy($order,$dir)
                            ->get();

            $totalFiltered = Product::where('id','LIKE',"%{$search}%")
                             ->orWhere('title', 'LIKE',"%{$search}%")
                             ->count();
        }

        $data = array();
        if(!empty($products))
        {
            foreach ($products as $product)
            {
                $show =  route('products.show',$product->id);
                $edit =  route('products.edit',$product->id);

                $all_products_data['id'] = $product->id;
                $all_products_data['title'] = $product->title;
                $all_products_data['body'] = substr(strip_tags($product->body),0,50)."...";
                $all_products_data['created_at'] = date('j M Y h:i a',strtotime($product->created_at));
                $all_products_data['options'] = " <a href='{$show}' title='SHOW' ><span class='glyphicon glyphicon-list'></span></a>
                                           <a href='{$edit}' title='EDIT' ><span class='glyphicon glyphicon-edit'></span></a>";
                $data[] = $all_products_data;

            }
        }
          
        $json_data = array(
                    "draw"            => intval($request->input('draw')),  
                    "recordsTotal"    => intval($totalData),  
                    "recordsFiltered" => intval($totalFiltered), 
                    "data"            => $data   
                    );
            
        echo json_encode($json_data); 
        
    }

Web Programming Tutorials Example with Demo

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about Server side Laravel DataTables example.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.


Related FAQ

Here are some more FAQ related to this Article:

  1.   Jquery Get Number of Characters String length
  2.   wheredate in laravel | Eloquent date filtering : whereDate()
  3.   Laravel 5.8 Datatables Server Side Processing
  4.   How to Solve Error call to a member function toArray() on null?
  5.   JavaScript setInterval and clearInterval function
  6.   PayPal Payment Gateway with PHP MySQL Database
  7.   Jquery Datatable listing json Server side in Php Mysql and Ajax
  8.   DataTables Pagination Server-side Processing using PHP and MySQL
  9.   DataTables Column Search Server side processing in PHP
  10.   WordPress Create Plugin From Scratch