Skip to content

Latest commit

 

History

History
82 lines (62 loc) · 1.71 KB

engine-collection.md

File metadata and controls

82 lines (62 loc) · 1.71 KB

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::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();
});