Skip to content

Latest commit

 

History

History
114 lines (81 loc) · 2.49 KB

edit-column.md

File metadata and controls

114 lines (81 loc) · 2.49 KB

Edit Column

You can edit a column on your response by using editColumn api.

Edit Column with Blade Syntax

use DataTables;

Route::get('user-data', function() {
	$model = App\User::query();

	return DataTables::eloquent($model)
				->editColumn('name', 'Hi {{$name}}!')
				->toJson();
});

Edit Column with Closure

use DataTables;

Route::get('user-data', function() {
	$model = App\User::query();

	return DataTables::eloquent($model)
				->editColumn('name', function(User $user) {
					return 'Hi ' . $user->name . '!';
				})
				->toJson();
});

Edit Column with View

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

use DataTables;

Route::get('user-data', function() {
	$model = App\User::query();

	return DataTables::eloquent($model)
				->editColumn('name', 'users.datatables.name')
				->toJson();
	
});

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

Hi {{ $name }}!

Edit Column with View and Data

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

use DataTables;

Route::get('user-data', function() {
	$model = App\User::query();

	$externalData = 'External';

	return DataTables::eloquent($model)
				->editColumn('name', ['users.datatables.name', [
					'externalData' => $externalData,
				]])
				->toJson();
	
});

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

Hi {{ $name }}!

Here is some external data: {{ $externalData }}.

Edit only the requested Columns

{tip} You can skip editing the columns that are not in your requested payload by using editOnlySelectedColumns before using editColumn

use DataTables;

Route::get('user-data', function() {
	$model = App\User::query();

	return DataTables::eloquent($model)
                ->editColumn('id', function () {
                    return view('users.datatables.id'); // View will always be rendered
                })
                ->editOnlySelectedColumns()
                ->editColumn('name', function () {
                    return 'Hi ' . $user->name . '!'; // View will only be rendered if the column is in the payload
                               only if in the payload
                })
                ->toJson();
});