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

 
Id Title Body Created At Options
Free Download Example - Pakainfo.com

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', 'ArticleController@allarticles' )->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'] = " ";
            $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.

Leave a Comment