Importing large CSV files into a database can be a common requirement in many applications. With Laravel 11, this process can be made efficient and straightforward. This guide will walk you through the steps needed to import a large CSV file into a database using Laravel 11. We will use Laravel’s Eloquent ORM and some additional packages to streamline the process.
Prerequisites
Before we begin, make sure you have the following installed:
- PHP 8.1 or higher
- Composer
- Laravel 11
- MySQL or any other database supported by Laravel
Step 1: Setting Up a New Laravel Project
First, create a new Laravel project. Open your terminal and run:
composer create-project --prefer-dist laravel/laravel csv-import-example
Navigate to the project directory:
cd csv-import-example
Step 2: Install Required Packages
For handling large CSV files efficiently, we will use the maatwebsite/excel
package. Install it using Composer:
composer require maatwebsite/excel
After installing, add the service provider and aliases to the config/app.php
file:
'providers' => [
...
Maatwebsite\Excel\ExcelServiceProvider::class,
],
'aliases' => [
...
'Excel' => Maatwebsite\Excel\Facades\Excel::class,
],
Step 3: Create a Migration for Your Data
Let’s create a migration for the table where we will import the CSV data. Run the following command:
php artisan make:migration create_users_table --create=users
Open the generated migration file in database/migrations
and define the table structure. For example:
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateUsersTable extends Migration
{
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('users');
}
}
Run the migration to create the table:
php artisan migrate
Step 4: Create a Model and Import Class
Next, create a model for the users
table:
php artisan make:model User
Now, create an import class using the maatwebsite/excel
package:
php artisan make:import UsersImport --model=User
Open the generated UsersImport
class in app/Imports
and modify it as follows:
namespace App\Imports;
use App\Models\User;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
class UsersImport implements ToModel, WithHeadingRow
{
public function model(array $row)
{
return new User([
'name' => $row['name'],
'email' => $row['email'],
]);
}
}
Step 5: Create a Controller
Create a controller to handle the file upload and import:
php artisan make:controller ImportController
Open the ImportController
and add the following methods:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Imports\UsersImport;
use Maatwebsite\Excel\Facades\Excel;
class ImportController extends Controller
{
public function showImportForm()
{
return view('import');
}
public function import(Request $request)
{
$request->validate([
'file' => 'required|mimes:csv,txt',
]);
Excel::import(new UsersImport, $request->file('file'));
return redirect()->back()->with('success', 'CSV file imported successfully!');
}
}
Step 6: Create a Blade View
Create a Blade view to upload the CSV file. In resources/views
, create a new file called import.blade.php
:
<!DOCTYPE html>
<html>
<head>
<title>Import CSV File</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>
<div class="container mt-5">
<div class="card">
<div class="card-header">
<h3>Import CSV File</h3>
</div>
<div class="card-body">
@if (session('success'))
<div class="alert alert-success">
{{ session('success') }}
</div>
@endif
<form action="{{ route('import') }}" method="POST" enctype="multipart/form-data">
@csrf
<div class="form-group">
<label for="file">Choose CSV File</label>
<input type="file" name="file" class="form-control" required>
</div>
<button type="submit" class="btn btn-primary">Import</button>
</form>
</div>
</div>
</div>
</body>
</html>
Step 7: Define Routes
Open the routes/web.php
file and add the following routes:
use App\Http\Controllers\ImportController;
Route::get('import-form', [ImportController::class, 'showImportForm']);
Route::post('import', [ImportController::class, 'import'])->name('import');
Step 8: Testing the Import Functionality
Start your Laravel development server:
php artisan serve
Navigate to http://localhost:8000/import-form
in your browser. You should see a form to upload a CSV file. Choose your CSV file and click the “Import” button. If everything is set up correctly, the data from the CSV file will be imported into your database.
Conclusion
Importing large CSV files into a database in Laravel 11 is made simple with the maatwebsite/excel
package. This guide covered setting up a new Laravel project, creating migrations, models, import classes, and controllers, and creating a simple form to upload and import CSV files. With these steps, you should be able to handle large CSV imports efficiently in your Laravel applications.
- Check our tools small Tools
- Check our tools website Word count