Skip to content
Icon

WARNING You're browsing the documentation for an old version of LARAVEL-DATATABLES. Consider upgrading your project to laravel-datatables 11.0.

Eloquent Data Source

You may use Laravel's Eloquent Model as data source for your dataTables. You can look at Yajra\DataTables\EloquentDataTable class which handles the conversion of your Eloquent Model into a readbale DataTable API response.

Eloquent via Factory

use DataTables;
 
Route::get('user-data', function() {
$model = App\User::query();
 
return DataTables::of($model)->toJson();
});

Eloquent via Facade

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

Eloquent via Dependency Injection

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

Eloquent via IoC

Route::get('user-data', function() {
$model = App\User::query();
 
return app('datatables')->eloquent($model)->toJson();
});

EloquentDataTable new Instance

use Yajra\DataTables\EloquentDataTable;
 
Route::get('user-data', function() {
$model = App\User::query();
 
return (new EloquentDataTable($model))->toJson();
});