Laravel How to upload EXCEL file to database?

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Laravel: How to Upload and Process Excel Files into Your Database (The Complete Guide)

Dealing with file uploads, especially structured formats like Excel, is a common hurdle for developers. While basic file handling seems straightforward, correctly processing a spreadsheet involves more than just saving a file to the server; it requires proper request handling, security checks, and intelligent parsing. If you are running into issues with simple examples, it often means you need a dedicated tool rather than trying to reinvent the wheel.

This guide will walk you through the correct, professional way to upload an Excel file in Laravel, moving beyond basic file saving to actual data import.

Why Simple Uploads Fail: Understanding the Mechanics

You mentioned struggling with your initial setup. The reason simple manual checks often fail is due to how HTTP requests handle multipart/form-data. When a file is uploaded via an HTML form, the controller receives the file stream in a specific way. Simply checking if a file exists requires careful mapping between the input name in your Blade view and what the controller expects.

For handling complex file types like Excel (.xlsx or .xls), manually reading the raw file contents and parsing the XML/binary structure is time-consuming, error-prone, and highly insecure. This is why the Laravel ecosystem provides powerful packages designed specifically for this task.

The Professional Solution: Using Maatwebsite/Excel

The most robust and recommended way to handle Excel imports in Laravel is by utilizing a dedicated package, such as Maatwebsite/Excel. This package abstracts away the complexity of reading spreadsheets, handling different file types, error management, and mapping rows directly into your Eloquent models.

This approach aligns perfectly with the principles of clean, maintainable code that robust frameworks like those promoted by laravelcompany.com advocate for.

Step 1: Installation

First, you need to install the package via Composer:

composer require maatwebsite/excel

Step 2: Setting Up the Controller Logic

Instead of manually checking hasFile(), you will use the package's methods within your controller to handle the file upload and processing. We will focus on saving the file temporarily and then using the package to read it.

Example Controller Implementation:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;
use App\Models\Student; // Assuming you have a Student model

class ExcelImportController extends Controller
{
    public function importExcel(Request $request)
    {
        // 1. Validate the request first! Always validate inputs.
        $request->validate([
            'excel_file' => 'required|file|mimes:xlsx,xls', // Ensure the file is uploaded and has correct extension
        ]);

        try {
            // 2. Store the file temporarily (optional, but good practice if you need to store it later)
            $path = $request->file('excel_file')->store('uploads');

            // 3. Use the Excel facade to import the file directly into a collection
            Excel::import(new \App\Imports\StudentImport, $request->file('excel_file'), 'uploads/' . $request->file('excel_file')->hashName);

            return response()->json(['success' => true, 'message' => 'Excel file imported successfully!']);

        } catch (\Exception $e) {
            // Handle any errors during import (e.g., wrong format, missing columns)
            return response()->json(['success' => false, 'error' => 'File import failed: ' . $e->getMessage()], 500);
        }
    }
}

Step 3: Creating the Import Class

The core logic for mapping Excel rows to database records resides in an Import class. This separates your file handling from your business logic, making the code much cleaner.

Example Import Class (app/Imports/StudentImport.php):

<?php

namespace App\Imports;

use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;

class StudentImport implements ToCollection
{
    /**
     * @param Collection $rows
     * @return Collection
     */
    public function collection(Collection $rows)
    {
        foreach ($rows as $row) {
            // Assuming your Excel has columns: Name, Email, Grade
            Student::create([
                'name' => $row['Name'],
                'email' => $row['Email'],
                'grade' => $row['Grade'],
                // Add other fields as needed
            ]);
        }
    }
}

Step 4: Updating the Blade View

Your Blade file needs to ensure the input name matches what your controller expects. Notice the use of enctype="multipart/form-data", which is crucial for file uploads.

Example Blade Snippet:

<form class="form-upload" method="POST" action="{{ url('/excel-import') }}" enctype="multipart/form-data">
    @csrf
    <div class="form-group">
        <label for="file">Excel Upload</label>
        {{-- IMPORTANT: 'excel_file' must match the input name you validate against --}}
        <input type="file" id="file" name="excel_file" required> 
        <p class="help-block">Please select an Excel file (.xlsx or .xls).</p>
    </div>
    <button type="submit" class="btn btn-primary">Import Data</button>
</form>

Conclusion

Relying on manual file handling for structured data imports is a recipe for bugs. By adopting established packages like Maatwebsite/Excel, you leverage the power of the Laravel ecosystem to create secure, efficient, and highly maintainable code. This strategy ensures that your application remains robust, which is a core principle emphasized by modern framework development, including what is championed on laravelcompany.com. Focus on using these tools, validate your inputs rigorously, and you will successfully handle any file import task with confidence.