![]()
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 Filtered Records
In some cases, we need to manually set the filtered records count of our DataTables and skip its internal counting functionality. To achieve this, use the setFilteredRecords($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) ->setFilteredRecords(100) ->toJson();});
Common Use Cases
Combined with Set Total Records
use Yajra\DataTables\Facades\DataTables;use App\Models\User; Route::get('user-data', function() { $model = User::query(); $totalCount = User::count(); $filteredCount = User::where('status', 'active')->count(); return DataTables::eloquent($model) ->setTotalRecords($totalCount) ->setFilteredRecords($filteredCount) ->toJson();});
Performance Optimization
use Yajra\DataTables\Facades\DataTables;use App\Models\User;use Illuminate\Support\Facades\Cache; Route::get('user-data', function() { $model = User::query(); $filteredCount = Cache::remember('active_users_count', 60, fn () => User::where('status', 'active')->count()); return DataTables::eloquent($model) ->setFilteredRecords($filteredCount) ->toJson();});
See Also
- Set Total Records - Manually set total records
- Skip Total Records - Skip total records (deprecated)