Laravel 12: Seeding Data Using Excel Import with PHPSpreadsheet
Looking to speed up your Laravel development by importing Excel data directly into your database? In this guide, you'll learn how to use PHPSpreadsheet with Laravel 12 seeders to import Excel (.xlsx) files during development

This method is intended for developers who want to seed their Laravel database using Excel files — not for end-user Excel uploads.
🧰 Prerequisites
Before we dive into the code, make sure you have:
- Laravel 12 installed
- Composer
- A MySQL (or compatible) database connected
Basic knowledge of Laravel seeders and artisan commands
Step1: Installing PHPSpreadsheet in Laravel 12
You can install PhpSpreadsheet via Composer:
composer require phpoffice/phpspreadsheet
Step2: Place Your Excel File
Create a folder in your Laravel project for storing Excel files, for example:
/storage/app/imports/users.xlsx
Now place your Excel file there. Let’s assume the file is named users.xlsx
.
Step3: Create Your Database Table and Model
Assume we want to import users from the Excel file.
Migration
php artisan make:migration create_users_table
// database/migrations/xxxx_xx_xx_create_users_table.php
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamps();
});
php artisan migrate
Model
php artisan make:model User
Step4: Create a Seeder to Handle Import
Now let’s create a seeder to Laravel database seeding with Excel file:
php artisan make:seeder UserImportSeeder
Inside the seeder:
// database/seeders/UserImportSeeder.php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
use PhpOffice\PhpSpreadsheet\IOFactory;
use App\Models\User;
use Storage;
class UserImportSeeder extends Seeder
{
public function run(): void
{
// Load the Excel file
$filePath = storage_path('app/imports/users.xlsx');
$spreadsheet = IOFactory::load($filePath);
$worksheet = $spreadsheet->getActiveSheet();
$rows = $worksheet->toArray();
// Skip header row
foreach (array_slice($rows, 1) as $row) {
// Example: Assuming columns are [Name, Email]
User::create([
'name' => $row[0],
'email' => $row[1],
]);
}
}
}
Step5: Register the Seeder
Add the seeder in DatabaseSeeder.php
:
// database/seeders/DatabaseSeeder.php
public function run(): void
{
$this->call(UserImportSeeder::class);
}
6 Run the Seeder
Now run the seeder to import the Excel data:
php artisan db:seed
✅ Final Tips
Always validate your Excel file before processing to prevent bad data from entering your database.
You can support
.xls
,.xlsx
,.csv
and other formats with PHPSpreadsheet.If your Excel is huge, consider chunking or queueing the process.
🧪 Sample users.xlsx Format
Make sure the headers match your column order in code.
🧾 Conclusion
By using PHPSpreadsheet with Laravel seeders, you can import Excel to MySQL in Laravel 12 effortlessly. This technique is perfect for migrating spreadsheet data, testing with real-world datasets, or setting up default content in your applications.
Need help exporting to Excel next? Drop a comment or check out our next tutorial!