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.

READ :  Sorting and Searching using Vuejs - Vuejs table sort pagination

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.

DataTables Server-side Processing in Laravel

Simple HTML table

[php]

Id Title Body Created At Options

[/php]

Simple javascript Source code

[php]

[/php]

Define a Laravel Routes

routes/web.php
[php]
Route::post(‘allmovies’, ‘MovieController@allMovies’ )->name(‘allmovies’);
[/php]

Movie model

Movie.php
[php]
Laravel define a simple Controller

MovieController.php
[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’] = “ 
“;
$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);

}
[/php]

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 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.

Leave a Comment