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