Server-side processing Laravel DataTables

Today, We want to share with you Server-side processing Laravel DataTables.In this post we will show you How to implement DataTables server-side in laravel 5.8, hear for Datatables Server Side Processing in Laravel 5.8 we will give you demo and example for implement.In this post, we will learn about Laravel 5.8 – DataTables Server-side Processing with an example.

Server-side processing Laravel DataTables

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

As I will cover this Post with live Working example to develop jQuery Datatables and Laravel Server-side implementation, so the laravel datatables large data structures for this example is following below.

productController.php

This example is a bit more comlex since I have columns that are foreign keys of the product table.

ajax()) {
        $columns     = ['products.id', 'member_name', 'location', 'category_name', 'status_name', 'date'];
        $draw        = $request->draw;
        $start       = $request->start; 
        $length      = $request->length;
        $column      = $request->order[0]['column'];
        $dir         = $request->order[0]['dir'];
        $searchValue = $request->search['value'];
        
        //Sets the current page
        Paginator::currentPageResolver(function () use ($start, $length) {
            return ($start / $length + 1);
        });
        $products = Product::whereHas('member', function($query) use ($searchValue) {
            $query->where('name', 'like', '%'.$searchValue.'%');
        })
        ->orWhereHas('location', function($query) use ($searchValue) {
            $query->where('location', 'like', '%'.$searchValue.'%');
        })
        ->orWhereHas('category', function($query) use ($searchValue) {
            $query->where('name', 'like', '%'.$searchValue.'%');
        })
        ->orWhereHas('status', function($query) use ($searchValue) {
            $query->where('name', 'like', '%'.$searchValue.'%');
        })
        ->orWhere('products.id', 'like', '%'.$searchValue.'%')
        ->orWhere('date', 'like', '%'.str_replace('/', '-', $searchValue).'%')
        ->leftJoin('members', 'products.member_id', '=', 'members.id')
        ->leftJoin('locations', 'products.location_id', '=', 'locations.id')
        ->leftJoin('categories', 'products.category_id', '=', 'categories.id')
        ->leftJoin('statuses', 'products.status_id', '=', 'statuses.id')
        ->select('members.name as member_name', 'products.*', 'locations.location', 'categories.name as category_name', 'statuses.name as status_name')
        ->orderBy($columns[$column], $dir)
        ->paginate($length);
        return [
            'draw' => $draw,
            'recordsTotal' => $products->total(),
            'recordsFiltered' => $products->total(),
            'data' => $products
        ];
    }
    return view('product/index');
}

product.js

Just so you can get the idea, your data might be different. Note that I changed the dataSrc to fit the data we return.

var table = $('table.products-table').DataTable({
    processing: true,
    serverSide: true,
    lengthChange: true,
    "ajax": {
        "url": '/products',
        "dataSrc": "data.data"
    },
    "columns": [
        { "data": "id", "name": "id",
            fnCreatedCell: function (nTd, sData, oData, iRow, iCol) {
                $(nTd).html(""+oData.id+"");
            }
        },
        { "data": "member_name", "name": "member_name",
            fnCreatedCell: function (nTd, sData, oData, iRow, iCol) {
                console.log(oData);
                if(oData.member_name) {
                    $(nTd).html(""+oData.member_name+"");
                }
            }
        },
        { "data": "location", "name": "location", "defaultContent": "" },
        { "data": "category_name", "name": "category_name" },
        { "data": "status_name", "name": "status_name" },
        { "data": "date", "name": "date" },
    ],
    dom: 'ip>',
    order: [ [0, 'desc'] ],
    buttons: [
        'csv',
        {
            extend: 'excelHtml5',
            title: 'Data export'
        },
        {
            extend: 'pdfHtml5',
            title: 'Data export'
        },
    ],
    pageLength: 25,
    lengthMenu: [25, 50, 75, 100],
    initComplete: function(settings, json) {
         table.buttons().container().appendTo( '.colsm6:eq(0)', table.table().container() );
         $('table.products-table').show();
    }
});
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 processing Laravel DataTables.
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.

Leave a Comment