Skip to content

Index Column

In some cases, you need to track the index of the records on your response. To achieve this, you can add an index column on your response by using addIndexColumn API.


Basic Usage

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

How It Works

Using addIndexColumn will add another column on your response with a column name that is set in the index_column configuration.

The default index column name is DT_RowIndex.


Customizing Row ID

If you want to customize the index used by the DT_RowIndex, you can use setRowId('COLUMN') to change the index number:

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

[!WARNING] Be careful with this option. An index should be unique and an integer to be useful for DataTables.


Example Response

{
"draw": 1,
"recordsTotal": 10,
"recordsFiltered": 3,
"data": [
{
"DT_RowIndex": 1,
"id": 1,
"name": "John Doe",
"email": "[email protected]"
},
{
"DT_RowIndex": 2,
"id": 2,
"name": "Jane Smith",
"email": "[email protected]"
}
]
}

See Also