Skip to content

Add Column

You can add a custom column to your response by using the addColumn api.

{note} added columns are assumed to be computed columns and not part of the database. Thus, search/sort will be disabled for those columns. If you need them, use the editColumn api instead.

Add Column with Blade Syntax

use DataTables;
 
Route::get('user-data', function() {
$model = App\User::query();
 
return DataTables::eloquent($model)
->addColumn('intro', 'Hi {{$name}}!')
->toJson();
});

Add Column with Closure

use DataTables;
 
Route::get('user-data', function() {
$model = App\User::query();
 
return DataTables::eloquent($model)
->addColumn('intro', function(User $user) {
return 'Hi ' . $user->name . '!';
})
->toJson();
});

Add Column with View

{tip} You can use view to render your added column by passing the view path as the second argument on addColumn api.

use DataTables;
 
Route::get('user-data', function() {
$model = App\User::query();
 
return DataTables::eloquent($model)
->addColumn('intro', 'users.datatables.intro')
->toJson();
});

Then create your view on resources/views/users/datatables/intro.blade.php.

Hi {{ $name }}!

Add Column with specific order

{tip} Just pass the column order as the third argument of addColumn api.

use DataTables;
 
Route::get('user-data', function() {
$model = App\User::query();
 
return DataTables::eloquent($model)
->addColumn('intro', 'Hi {{$name}}!', 2)
->toJson();
});