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.

Resource Response

DataTables response using Laravel model resource.


Basic Usage

<?php
 
use Yajra\DataTables\Facades\DataTables;
use App\Models\User;
 
Route::get('user-data', function() {
return DataTables::eloquent(User::query())
->toJson();
});

[!TIP] When using Laravel API Resources with DataTables, you can wrap the result of UserResource::collection() and pass it to DataTables::of(). However, for most use cases with Eloquent models, using DataTables::eloquent() directly is recommended as it handles pagination, sorting, and filtering automatically.


Using with API Resources

If you need to transform your response using Laravel API Resources:

<?php
 
use Yajra\DataTables\Facades\DataTables;
use App\Models\User;
use App\Http\Resources\UserResource;
 
Route::get('user-data', function() {
return DataTables::eloquent(User::query())
->setTransformer(function (User $item) {
return UserResource::make($item)->resolve();
})
->toJson();
});

Example Response

{
"draw": 10,
"recordsTotal": 10,
"recordsFiltered": 10,
"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
}
]
}

See Also