Laravel Datatable Pagination Sorting and Search server side processing

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.

List of all Google Adsense, VueJS, AngularJS, PHP, Laravel Examples.

Snippet Contents

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.

Include DataTable External CDN


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 :

Summary

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

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.


Related FAQ

Here are some more FAQ related to this Article:

  1.   String Methods List using JavaScript Example
  2.   clear cache laravel
  3.   Hyperlinks not working with jquery mobile
  4.   Angular 6 Image Upload with Validation Example
  5.   PHP Laravel 6 DataTables Tutorial
  6.   Laravel 5.8 Datatables Server Side Processing
  7.   laravel print last query | Executed | Sql | php | tutorials
  8.   Implement Yajra Datatables in Laravel 7 Application
  9.   Create array in laravel 5
  10.   How to use Datatables in Laravel 5.8

Leave a Reply

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