Limit Pagination
The limit() API allows you to implement custom pagination using only LIMIT without OFFSET. This is useful when you want to implement cursor-based pagination or infinite scroll patterns.
Basic Usage
use Yajra\DataTables\Facades\DataTables;use Illuminate\Support\Facades\DB; Route::get('user-data', function() { $query = DB::table('users')->select('*'); return DataTables::query($query) ->limit(function (Builder $builder) { // Custom LIMIT logic $builder->limit(100); }) ->toJson();});
Cursor-Based Pagination
Implement cursor-based pagination by tracking the last seen ID:
use Yajra\DataTables\Facades\DataTables;use Illuminate\Support\Facades\DB; Route::get('user-data', function() { $lastId = request()->input('cursor'); $query = DB::table('users') ->select('*') ->orderBy('id'); if ($lastId) { $query->where('id', '>', $lastId); } return DataTables::query($query) ->limit(function (Builder $builder) { $builder->limit(50); }) ->toJson();});
Time-Based Pagination
Paginate through records based on timestamps:
use Yajra\DataTables\Facades\DataTables;use Illuminate\Support\Facades\DB;use Carbon\Carbon; Route::get('activity-data', function() { $before = request()->input('before'); $query = DB::table('activities') ->select('*') ->orderBy('created_at', 'desc'); if ($before) { $query->where('created_at', '<', Carbon::parse($before)); } return DataTables::query($query) ->limit(function (Builder $builder) { $builder->limit(25); }) ->toJson();});
Comparison
| Feature | Standard Pagination | LIMIT Only |
|---|---|---|
| OFFSET support | ✅ Yes | ❌ No |
| Cursor-based | ❌ No | ✅ Yes |
| Infinite scroll | Limited | ✅ Perfect |
| Performance at scale | Degrades | ✅ Constant |
See Also
- Skip Paging - Skip pagination entirely
- General Settings - Configuration options