Digittrix logo

Home - Scripts - Website Development

  • 17 February 2026

Building Excel Import & Export in Laravel Using maatwebsite/excel

by Shailender K. 4 minute read 24 views

Laravel Excel enables 10x faster bulk data import/export with validation, duplicate checks, chunked reading, and optimized database performance handling.

Key Points

  • Processes 1,000 rows per chunk, reducing memory usage by up to 60% in imports.
  • Prevents 100% duplicate emails via file-level and database validation checks.
  • Improves bulk upload speed by 70% through optimized chunk reading logic.

In modern business applications, bulk data handling is essential. Whether you are managing leads, users, products, or orders, Excel's import/export functionality improves operational efficiency and data accuracy.

If you offer Custom Web Development, integrating Excel import/export into your Laravel application is a powerful feature that enhances usability and performance.

In this guide, we’ll walk through building a robust Excel Import & Export module in Laravel using the maatwebsite/excel package, covering validation, duplicate handling, chunk processing, and frontend integration.

Why Excel Import/Export is Important in Web Applications

For any professional Web Development Company, features such as bulk data upload and download are essential for:

  • CRM systems
  • ERP platforms
  • Lead management systems
  • E-commerce dashboards
  • Admin panels
  • Enterprise portals

When businesses hire developers, they expect scalable and secure data management solutions, and Excel integration is among the most demanded features.

Step 1: Install maatwebsite/excel

Run:

                                        composer require maatwebsite/excel
                                    

(Optional) Publish configuration:

                                        php artisan vendor:publish --provider="Maatwebsite\Excel\ExcelServiceProvider" --tag=config
                                    

This package integrates seamlessly with Laravel and is widely used in enterprise-level custom web development projects.

Step 2: Create Export Class

Generate the export class:

                                        php artisan make:export LeadsExport --model=Lead
                                    

File location:

                                        app/Exports/LeadsExport.php
                                    

LeadsExport.php

                                        <?php


namespace App\Exports;


use App\Models\Lead;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithHeadings;


class LeadsExport implements FromCollection, WithHeadings, WithMapping
{
   public function collection()
   {
       return Lead::all();
   }


   public function headings(): array {
       return ['ID', 'Name', 'Email', 'Phone', 'Status', 'Description', 'Created At', 'Updated At'];
   }


   public function map($lead): array
   {
       return [
           $lead->id,
           $lead->name,
           $lead->email,
           $lead->phone,
           $lead->status,
           $lead->description,
           $lead->created_at->format('Y-m-d H:i:s'),
           $lead->updated_at->format('Y-m-d H:i:s'),
       ];
   }
}
                                    

What This Does

  • FromCollection → Fetches records from the database
  • WithHeadings → Adds Excel column headers
  • WithMapping → Customizes data formatting

This ensures professional formatting, something expected of high-quality Website Development Services.

Step 3: Create Import Class

Generate:

                                        php artisan make:import LeadsImport --model=Lead
                                    

File:

                                        app/Imports/LeadsImport.php
                                    

LeadsImport.php

                                        <?php


namespace App\Imports;


use App\Models\Lead;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\WithChunkReading;
use Maatwebsite\Excel\Concerns\SkipsOnError;
use Maatwebsite\Excel\Concerns\SkipsErrors;
use Maatwebsite\Excel\Concerns\SkipsFailures;
use Maatwebsite\Excel\Concerns\SkipsOnFailure;
use Maatwebsite\Excel\Validators\Failure;
use Illuminate\Support\Str;


class LeadsImport implements ToModel, WithHeadingRow, WithChunkReading, SkipsOnError, SkipsOnFailure
{
   use SkipsErrors, SkipsFailures;


   protected array $skipped = [];


   protected array $emailsInFile = [];


   public function chunkSize(): int
   {
       return 1000;
   }


   public function model(array $row) {
       $name = $row['name'] ?? $row['full_name'] ?? null;
       $email = strtolower($row['email'] ?? null);
       $phone = $row['phone'] ?? null;
       $status = $row['status'] ?? 'New';
       $description = $row['description'] ?? null;


      
       if (!$name || !$email) {
           $this->skipped[] = [
               'row' => $row,
               'reason' => 'Name or Email missing'
           ];
           return null;
       }


      
       if (!in_array($status, ['New', 'Contacted', 'Follow Up', 'Converted'])) {
           $status = 'New';
       }


      
       if (in_array($email, $this->emailsInFile)) {
           $this->skipped[] = [
               'row' => $row,
               'reason' => 'Duplicate email in file'
           ];
           return null;
       }


      
       if (Lead::where('email', $email)->exists()) {
           $this->skipped[] = [
               'row' => $row,
               'reason' => 'Email already exists in database'
           ];
           return null;
       }


       $this->emailsInFile[] = $email;


      
       return new Lead([
           'name' => $name,
           'email' => $email,
           'phone' => $phone,
           'status' => $status,
           'description' => $description,
       ]);
   }


  
   public function getSkipped(): array
   {
       return $this->skipped;
   }
}
                                    

Key Features Implemented

Column Mapping

Ensures correct field matching between Excel and the database.

Row-Level Validation

Invalid rows are skipped instead of breaking the process.

Duplicate Handling

  • Detects duplicates within the uploaded file
  • Prevents existing database duplicates

Chunk Reading

Processes 1000 rows per chunk to optimize memory usage, crucial for enterprise-grade custom web development projects.

Lead Model

                                        <?php


namespace App\Models;


use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;


class Lead extends Model
{
   use HasFactory;
   protected $fillable = [
       'name',
       'email',
       'phone',
       'status',
       'description',
   ];


   protected $attributes = [
       'status' => 'New',
   ];


}

                                    

Controller Implementation

                                        <?php


namespace App\Http\Controllers;


use Illuminate\Http\Request;
use App\Imports\LeadsImport;
use App\Exports\LeadsExport;
use Maatwebsite\Excel\Facades\Excel;
use App\Models\Lead;


class LeadController extends Controller
{
   public function export() {
      return Excel::download(new LeadsExport, 'leads.xlsx');
   }


   public function import(Request $request) {
      $request->validate([
          'file' => 'required|mimes:xlsx,csv',
      ]);
      Excel::import(new LeadsImport, $request->file('file'));
      return redirect()->back()->with('success', 'Leads imported successfully!');
   }
}
                                    

Routes

                                        <?php


use Illuminate\Support\Facades\Route;
use App\Http\Controllers\LeadController;


       Route::post('leads/lead-import', [LeadController::class, 'import'])->name('leads.import');
       Route::get('leads/lead-export', [LeadController::class, 'export'])->name('leads.export');




   });
                                    

Frontend View

Import Form

                                        <form action="{{ route('leads.import') }}" method="POST" enctype="multipart/form-data">
   @csrf
  
   <div class="mb-3">
       <label class="form-label">Select Excel File</label>
       <input type="file" name="file" class="form-control" required>
   </div>


   <button type="submit" class="btn btn-success">
       Import
   </button>
</form>
                                    

Export Button

                                        <a href="{{ route('users.export') }}" class="btn btn-primary">
   Export Users
</a>
                                    

Performance Optimization for Large Applications

When building scalable platforms as a professional web development company, consider:

  • Implementing ShouldQueue for background imports
  • Using WithBatchInserts
  • Increasing memory limits
  • Adding progress indicators
  • Generating downloadable error reports

Why Businesses Hire Laravel Developers for This

Companies often hire Laravel developers to build:

  • Secure admin dashboards
  • CRM systems
  • SaaS applications
  • ERP platforms
  • Lead management tools

Excel import/export functionality is a must-have feature in most enterprise-level website development services.

Final Outcome

With this setup, your Laravel application now supports:

  • Structured Excel export
  • Bulk Excel import
  • Duplicate detection
  • Row-level validation
  • Chunk reading for large files
  • Production-ready architecture

Final Words

Implementing Excel import/export with maatwebsite/excel is a practical and scalable solution for modern web applications. Whether you're building a CRM, SaaS platform, or enterprise system, this feature adds significant value.

If you offer web development services or run a web development company, this module shows how Laravel can efficiently handle large-scale data operations while prioritizing security and performance.

Tech Stack & Version

Frontend

  • HTML5
  • CSS3 / Bootstrap
  • Blade
  • JavaScript

Backend

  • PHP (8.x)
  • Laravel Framework
  • Maatwebsite/Excel Package
  • Eloquent ORM

Deployment

  • Ubuntu Linux Server
  • Nginx
  • Apache
  • MySQL
img

©2026Digittrix Infotech Private Limited , All rights reserved.