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 readbale 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::queryBuilder($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->queryBuilder($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')->queryBuilder($collection)->toJson();
});

CollectionDataTable new Instance


use Yajra\DataTables\CollectionDataTable;

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

    return (new CollectionDataTable($collection))->toJson();
});