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.

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" }
            ]  

        });
    });

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

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.

  Server-side processing Laravel DataTables

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 :

  Laravel 6 Inserting & Updating Eloquent Models

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.   how to insert data using ajax in laravel with datatables?
  2.   Laravel DataTables Server side Processing Example
  3.   onclick update database in laravel
  4.   laravel datatables Server-side Processing
  5.   Laravel Route pass multiple Parameters Examples
  6.   PHP Laravel 6 DataTables Tutorial
  7.   Laravel Datatables edit Column HTML Source Code render
  8.   Laravel 5.8 Datatables Server Side Processing
  9.   jQuery Slide Effect toggle Direction - slideToggle Animation
  10.   How to use Datatables in Laravel 5.8

Leave a Reply

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