How to add row number or Serial no in laravel datatable
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
# How to Add Row Number or Serial No in Laravel DataTables
When presenting large datasets through a DataTables interface, providing a sequential identifier—a row number or serial number—is essential for user experience and data auditing. As a senior developer working within the Laravel ecosystem, you often need to perform these kinds of data manipulations efficiently before rendering the front-end view.
This guide will walk you through the most practical and efficient ways to add a custom `serial_no` field to your dataset retrieved from the database and integrate it seamlessly into your DataTables implementation.
## Understanding the Requirement
You have a list of records from the `bill_info` table, and you need to generate a sequence (1, 2, 3, ...) for each row. Your initial approach using `$data = BillInfo::get(['bill_info.*']);` retrieves the raw data, but it doesn't automatically include this sequence. The challenge is generating this sequence *within* your Laravel application logic before passing the data to the DataTables library.
## Method 1: Generating Serial Numbers Using Collection Mapping (Recommended)
The most straightforward and readable way in PHP/Laravel is to fetch the data and then use a simple loop or collection mapping function to assign the serial number. This method is highly flexible, regardless of the underlying SQL dialect.
### Step-by-Step Implementation
1. **Retrieve the Data:** Fetch all necessary records from your Eloquent model.
2. **Assign the Sequence:** Iterate over the resulting collection and add a new attribute for `serial_no`.
Here is how you can modify your data retrieval process:
```php
use App\Models\BillInfo;
use Yajra\DataTables\Facades\DataTables;
// 1. Retrieve the raw data
$billInfos = BillInfo::orderBy('id')->get();
// 2. Map and assign the serial number
$dataWithSerial = $billInfos->map(function ($item, $index) {
// The index starts from 0, so we add 1 for the serial number
$item->serial_no = $index + 1;
return $item;
});
// 3. Pass the modified data to DataTables
return DataTables::of($dataWithSerial)
->removeColumn('id') // Assuming you want to remove the primary ID if serial_no is sufficient
->addColumn('serial_no', function ($row) {
return $row->serial_no;
})
->make(true);
```
### Explanation and Best Practices
By using the `map` function on the Eloquent collection, we are transforming each model instance into a new structure that includes the desired `serial_no`. This ensures that the sequence is correctly attached to every record before DataTables processes it. This pattern aligns perfectly with how you build robust data pipelines in Laravel, emphasizing clean separation between data retrieval (Eloquent) and presentation logic.
## Method 2: Using Database Window Functions (Advanced/Performance Focused)
For extremely large datasets where performance under heavy load is critical, generating the serial number directly in the database query using window functions can be more efficient than fetching all rows into PHP memory first. This delegates the numbering task to the highly optimized SQL engine.
If your database supports it (e.g., MySQL 8+, PostgreSQL), you can use `ROW_NUMBER()`:
```php
use Illuminate\Support\Facades\DB;
// Retrieve data using a CTE or subquery for numbering
$data = DB::table('bill_info')
->select(DB::raw('*, ROW_NUMBER() OVER (ORDER BY id) as serial_no'))
->get();
return DataTables::of($data)
->removeColumn('id')
->make(true);
```
This approach is powerful because it minimizes data transfer, but it requires a deeper understanding of SQL. If you are building complex systems, leveraging these advanced database features, as promoted by the framework philosophy in **Laravel**, leads to more scalable solutions.
## Conclusion
For most standard applications, **Method 1 (Collection Mapping)** offers the best balance of readability, maintainability, and ease of implementation when working with Laravel DataTables setups. It keeps your application logic clean while achieving the desired result of adding a serial number to your list view. If you encounter performance bottlenecks with massive tables, then exploring database-level solutions like window