Laravel Datatable Pagination Sorting and Search server side processing

1.5/5 - (2 votes)

Today, We want to share with you Laravel Datatable Pagination Sorting and Search server side processing.In this post we will show you laravel install yajra datatables, hear for Implement datatables server side processing Laravel we will give you demo and example for implement.In this post, we will learn about how to insert data using ajax in laravel with datatables with an example.

Laravel Datatable Pagination Sorting and Search server side processing

There are the Following The simple About Laravel Datatable Pagination Sorting and Search server side processing Full Information With Example and source code.

As I will cover this Post with live Working example to develop Laravel 5.7 datatables with server-side, so the Jquery Datatables server-side processing with Laravel for this example is following below.

Read Also:  require is not defined - how to fix require is not defined error in JavaScript?

Include DataTable External CDN


Free Live Chat for Any Issue

HTML table

 <div class="row">
  <div class="col-md-12">
               <table class="table table-bordered" id="articles">
                    <thead>
                           <th>Id</th>
                           <th>Title</th>
                           <th>Body</th>
                           <th>Created At</th>
                           <th>Options</th>
                    </thead>        
               </table>
        </div>
</div>
<a href="https://www.pakainfo.com/" target="_blank" title="pakainfo">Free Download Example - Pakainfo.com</a>

javascript code

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

        });
    });

routes/web.php

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

Article model in Laravel

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Article extends Model
{
    
}

allArticle function in Laravel ArticleController.

public function allarticles(Request $request)
{
    
    $columns = array( 
                        0 =>'id', 
                        1 =>'title',
                        2=> 'body',
                        3=> 'created_at',
                        4=> 'id',
                    );

    $totalData = Article::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')))
    {            
        $articles = Article::offset($start)
                     ->limit($limit)
                     ->orderBy($order,$dir)
                     ->get();
    }
    else {
        $get_search = $request->input('search.value'); 

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

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

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

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

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

Angular 6 CRUD Operations Application Tutorials

Read :

Read Also:  onclick update database in laravel

Summary

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

Download

I hope you get an idea about Laravel Datatable Pagination Sorting and Search server side processing.
I would like to have feedback on my Pakainfo.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.