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.

Query Builder Data Source

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

Query Builder via Factory

use DB;
use DataTables;
 
Route::get('user-data', function() {
$query = DB::table('users');
 
return DataTables::of($query)->toJson();
});

Query Builder via Facade

use DB;
use DataTables;
 
Route::get('user-data', function() {
$query = DB::table('users');
 
return DataTables::queryBuilder($query)->toJson();
});

Query Builder via Dependency Injection

use DB;
use Yajra\DataTables\DataTables;
 
Route::get('user-data', function(DataTables $dataTables) {
$query = DB::table('users');
 
return $dataTables->queryBuilder($query)->toJson();
});

Query Builder via IoC

use DB;
 
Route::get('user-data', function() {
$query = DB::table('users');
 
return app('datatables')->queryBuilder($query)->toJson();
});

QueryDataTable new Instance

use Yajra\DataTables\QueryDataTable;
 
Route::get('user-data', function() {
$query = DB::table('users');
 
return (new QueryDataTable($query))->toJson();
});