Skip to content

Set Collection Offset

The setOffset() API defines the offset of the first item in the collection relative to the full dataset. This is useful when working with pre-sliced collections.


Basic Usage

use Yajra\DataTables\Facades\DataTables;
 
Route::get('user-data', function() {
$collection = collect([
['id' => 11, 'name' => 'John'],
['id' => 12, 'name' => 'Jane'],
['id' => 13, 'name' => 'James'],
]);
 
return DataTables::collection($collection)
->setOffset(10) // First item is at position 10 in the full dataset
->toJson();
});

Use Case: Pre-Sliced Data

When you have a collection that's already been sliced from a larger dataset:

use Yajra\DataTables\Facades\DataTables;
 
Route::get('page-data', function() {
// Get page 2 of a pre-sliced collection (10 items per page)
$page = request()->input('page', 1);
$perPage = 10;
 
// Get the full dataset and slice for current page
$fullCollection = cache()->remember('all-users', 3600, function () {
return User::all()->toArray();
});
 
$sliced = collect($fullCollection)
->slice(($page - 1) * $perPage, $perPage);
 
return DataTables::collection($sliced)
->setOffset(($page - 1) * $perPage)
->toJson();
});

With Index Column

The offset affects the row index when using addIndexColumn():

use Yajra\DataTables\Facades\DataTables;
 
Route::get('user-data', function() {
$collection = collect([
['id' => 11, 'name' => 'John'],
['id' => 12, 'name' => 'Jane'],
]);
 
return DataTables::collection($collection)
->setOffset(10)
->addIndexColumn()
->toJson();
});
 
// Result: DT_RowIndex will be 11, 12 instead of 1, 2

See Also