Laravel Dynamically Adding Multiple Columns in datatable server side

Today, We want to share with you Laravel Dynamically Adding Multiple Columns in datatable server side.In this post we will show you wordpress plugin require another plugin, hear for how make dynamic column datatable server side with laravel we will give you demo and example for implement.In this post, we will learn about Laravel Datatables – Dynamically add a column and enable searching on server side with an example.

Laravel Dynamically Adding Multiple Columns in datatable server side

There are the Following The simple About Laravel 6 DataTables – Dynamic Columns From Ajax Data Source? Full Information With Example and source code.

As I will cover this Post with live Working example to develop Implementing jquery server side datatable in laravel with dynamic table, so the how to get the dynamic column header and result from ajax call in jquery datatable, is used for this example is following below.

READ :  C# Factorial program Tutorial with Examples

DataTables Server-side Processing in Laravel

Simple HTML table

<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/dt-1.10.12/datatables.min.css"/>
<script type="text/javascript" src="https://cdn.datatables.net/v/dt/dt-1.10.12/datatables.min.js"></script>

<div class="row">
	<div class="col-md-12">
               <table class="table table-bordered" id="movies">
                    <thead>
                           <th>Id</th>
                           <th>Title</th>
                           <th>Body</th>
                           <th>Created At</th>
                           <th>Options</th>
                    </thead>				
               </table>
        </div>
</div>

Simple javascript Source code

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

        });
    });
</script>

Define a Laravel Routes

E-junkie: Sell digital downloads online

E-junkie Provides a Copy-paste buy-now, and cart buttons for selling downloads, codes and tangible products on any website, blog, social media, email and messenger!

Also see:

  1. The Top 10+ Best Webinar Software Platforms For 2020-2021
  2. Build Your Future Godaddy Careers And Jobs
  3. Introduction To Web Hosting Services

routes/web.php

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

Movie model

Movie.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Movie extends Model
{
    
}

Laravel define a simple Controller

MovieController.php

public function allMovies(Request $request)
    {
        
        $columns = array( 
                            0 =>'id', 
                            1 =>'title',
                            2=> 'body',
                            3=> 'created_at',
                            4=> 'id',
                        );
  
        $totalData = Movie::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')))
        {            
            $movies = Movie::offset($start)
                         ->limit($limit)
                         ->orderBy($order,$dir)
                         ->get();
        }
        else {
            $search = $request->input('search.value'); 

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

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

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

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

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

Web Programming Tutorials Example with Demo

Read :

READ :  Laravel Create Database Migration and Model

Summary

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

I hope you get an idea about Laravel Dynamically generated columns with addColumn method.
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.