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.

Collection Data Source

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

Collection via Factory

use DataTables;
 
Route::get('user-data', function() {
$collection = collect([
['id' => 1, 'name' => 'John'],
['id' => 2, 'name' => 'Jane'],
['id' => 3, 'name' => 'James'],
]);
 
return DataTables::of($collection)->toJson();
});

Collection via Facade

use DataTables;
 
Route::get('user-data', function() {
$collection = collect([
['id' => 1, 'name' => 'John'],
['id' => 2, 'name' => 'Jane'],
['id' => 3, 'name' => 'James'],
]);
 
return DataTables::collection($collection)->toJson();
});

Collection via Dependency Injection

use Yajra\DataTables\DataTables;
 
Route::get('user-data', function(DataTables $dataTables) {
$collection = collect([
['id' => 1, 'name' => 'John'],
['id' => 2, 'name' => 'Jane'],
['id' => 3, 'name' => 'James'],
]);
 
return $dataTables->collection($collection)->toJson();
});

Collection via IoC

 
Route::get('user-data', function() {
$collection = collect([
['id' => 1, 'name' => 'John'],
['id' => 2, 'name' => 'Jane'],
['id' => 3, 'name' => 'James'],
]);
 
return app('datatables')->collection($collection)->toJson();
});

CollectionDataTable new Instance

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