Skip to content
Icon

WARNING You're browsing the documentation for an upcoming version of Laravel DataTables. The documentation and features of this release are subject to change.

Additional Data Response

You can add additional server data to your response using the with API.


Adding Response using Key and Value

use Yajra\DataTables\Facades\DataTables;
use App\Models\User;
 
Route::get('user-data', function() {
$model = User::query();
 
return DataTables::eloquent($model)
->with('posts', 100)
->with('comments', 20)
->toJson();
});

Adding Response using Array

use Yajra\DataTables\Facades\DataTables;
use App\Models\User;
 
Route::get('user-data', function() {
$model = User::query();
 
return DataTables::eloquent($model)
->with([
'posts' => 100,
'comments' => 20,
])
->toJson();
});

Adding Response with Closure

use Yajra\DataTables\Facades\DataTables;
use App\Models\User;
 
Route::get('user-data', function() {
$model = User::query();
 
return DataTables::eloquent($model)
->with('count', function() use ($model) {
return $model->count();
})
->toJson();
});

Adding Response with Query Callback

[!NOTE] This is for Query and Eloquent instance only.

use Yajra\DataTables\Facades\DataTables;
use App\Models\User;
 
Route::get('user-data', function() {
$model = User::query();
 
return DataTables::eloquent($model)
->withQuery('count', function($filteredQuery) {
return $filteredQuery->count();
})
->toJson();
});

Example Response

{
"draw": 1,
"recordsTotal": 10,
"recordsFiltered": 3,
"data": [
{
"id": 476,
"name": "Esmeralda Kulas",
"email": "[email protected]",
"created_at": "2016-07-31 23:26:14",
"updated_at": "2016-07-31 23:26:14",
"deleted_at": null,
"superior_id": 0
},
{
"id": 6,
"name": "Zachery Muller",
"email": "[email protected]",
"created_at": "2016-07-31 23:25:43",
"updated_at": "2016-07-31 23:25:43",
"deleted_at": null,
"superior_id": 1
}
],
"posts": 100,
"comments": 20
}

See Also