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.

Set Total Records

In some cases, we need to manually set the total records of our DataTables and skip its internal counting functionality. To achieve this, use the setTotalRecords($count) API.


Basic Usage

use Yajra\DataTables\Facades\DataTables;
use App\Models\User;
 
Route::get('user-data', function() {
$model = User::query();
 
return DataTables::eloquent($model)
->setTotalRecords(100)
->toJson();
});

Common Use Cases

Pre-calculated Count

use Yajra\DataTables\Facades\DataTables;
use App\Models\User;
 
Route::get('user-data', function() {
$model = User::query();
$totalCount = User::count();
 
return DataTables::eloquent($model)
->setTotalRecords($totalCount)
->toJson();
});

Cache Count

use Yajra\DataTables\Facades\DataTables;
use App\Models\User;
use Illuminate\Support\Facades\Cache;
 
Route::get('user-data', function() {
$model = User::query();
$totalCount = Cache::remember('users_count', 60, fn () => User::count());
 
return DataTables::eloquent($model)
->setTotalRecords($totalCount)
->toJson();
});

See Also