laravel Excel Import set header on column
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Mastering Data Import: Setting Headers Correctly with Laravel Excel
As developers utilizing frameworks like Laravel, efficiently managing data migration and import is a daily necessity. One of the most powerful tools for this task in the PHP ecosystem is the maatwebsite/excel package, commonly known as Laravel Excel. When dealing with external spreadsheets, correctly mapping headers to your application logic is crucial.
Recently, I encountered a common stumbling block when implementing imports: attempting to use the WithHeadingRow trait while trying to access data using mixed indexing methods, leading to errors like "Undefined offset." This post will walk you through the exact cause of this issue and provide a robust solution for setting headers correctly in your Laravel Excel imports.
The Pitfall of Misaligned Headers
You are attempting to use the WithHeadingRow trait, which signals to the importer that the first row of the spreadsheet contains column headers. When this is active, Laravel Excel expects you to retrieve data from the collection using those header names as keys (e.g., $row['Column Name']).
The error you encountered—Undefined offset: 0—arose because your code was mixing positional access ($row[0]) with associative access ($row['unit_type']). When WithHeadingRow is active, the structure of the input array changes based on whether headers are present. If you rely on numeric indexing for one part of the import and associative indexing for another, the importer gets confused about the expected data shape, leading to unpredictable results and errors.
The Solution: Strict Adherence to WithHeadingRow
To resolve this, the key is consistency. If you use WithHeadingRow, you must exclusively access the cell values using the exact header names found in your Excel file.
In your specific case, if your Excel sheet has a column named "Unit Type," you must reference it as $row['Unit Type'] instead of trying to rely on positional indices like $row[0] or assuming the next column is always at index 1.
Here is how we correct the logic within your collection method:
Corrected Implementation Example
Let’s assume your Excel headers are exactly "Unit Type" and you want to use that value for creation.
<?php
namespace App\Imports;
use App\UnitType;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
class UnitTypesImport implements ToCollection, WithHeadingRow
{
protected $user_id;
protected $proj_id;
public function __construct($user_id, $proj_id)
{
$this->user_id = $user_id;
$this->proj_id = $proj_id;
}
public function collection(Collection $rows)
{
foreach ($rows as $row)
{
// Accessing data directly using the header name defined in Excel
$unitTypeName = $row['Unit Type'];
// Use the correctly retrieved value for logic
$unit_types_count = UnitType::where('name', $unitTypeName)
->where('project_id', $this->proj_id)
->count();
if ($unit_types_count == 0){
UnitType::create([
'name' => $unitTypeName, // Use the clean header value
'created_by' => $this->user_id,
'project_id' => $this->proj_id,
]);
}
}
}
public function headingRow(): int
{
// This correctly informs the importer that row 1 (index 0) contains headers.
return 1;
}
}
Key Takeaways for Robust Imports
- Consistency is King: When using
WithHeadingRow, treat every row as an associative array where keys are the column headers from your Excel file. - Use Headers Directly: Always use
$row['Your Header Name']instead of trying to guess the numeric index ($row[0]). This makes your code resilient to changes in column order within the spreadsheet, provided the header names remain consistent. - Setting
headingRow(): Ensure yourheadingRow()method correctly reports the row number containing the headers (remember that array indexing starts at 0, so if you have a header row, it is row index 0).
Conclusion
Importing data from Excel should be straightforward when you respect the contract established by traits like WithHeadingRow. By strictly using the column names provided in the spreadsheet as keys for accessing data, you eliminate ambiguity and prevent those frustrating "Undefined offset" errors. Mastering this pattern ensures your data processing logic remains clean, predictable, and scalable, aligning perfectly with best practices in building robust applications on Laravel. For more deep dives into leveraging the power of the Laravel ecosystem, always explore resources like https://laravelcompany.com.