Skip to content

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

Query Builder via IoC

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

QueryDataTable new Instance

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