Laravel : How to Import Excel file to Database

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Laravel: The Definitive Guide to Importing Excel Files into Your Database

Importing bulk data from spreadsheets like Excel or CSV files is a common requirement for web applications. When developers attempt this process in Laravel, the complexity often lies not in the framework itself, but in correctly handling file uploads, parsing the structured data, and ensuring seamless database insertion. If your import isn't working, it’s usually a mismatch between how the Excel file is formatted and how your PHP code expects that data to look.

As a senior developer, I can tell you that successfully importing Excel files requires careful attention to detail regarding libraries, data mapping, and error handling. Let’s walk through the best practices for achieving this in a robust Laravel application.

Prerequisites: Choosing the Right Tools

To handle spreadsheet imports efficiently in Laravel, relying on built-in PHP functions is inefficient. We need a dedicated package that can read complex Excel formats (XLSX) and convert them into usable PHP arrays. The community standard for this task is the Maatwebsite/Excel package. This library handles the complexities of reading different sheet types and formatting issues, saving you significant development time.

First, ensure you have installed the necessary package via Composer:

composer require maatwebsite/excel

This package provides the functionality needed to read the Excel file into a structured format that Laravel can easily process.

Step-by-Step Guide to Importing Data

The process involves three main steps: uploading the file, reading the file content, and inserting the data into the database.

1. Setting up the View (File Upload)

Your Blade view needs a proper HTML form to allow users to select the file. Notice how you correctly use enctype="multipart/form-data" (though often implied by the form structure) and ensure your controller expects the file via Input::file().

<form style="border: 4px solid #a1a1a1;margin-top: 15px;padding: 10px;" action="{{ URL::to('importExcel') }}" method="post" enctype="multipart/form-data">
    <input type="file" name="import_file" required>
    {!! Form::token(); !!}
    <button class="btn btn-primary" type="submit">Import File</button>
</form>

2. Refining the Controller Logic (The Import)

The key to avoiding import failures is robust data mapping. When you use Excel::load()->toArray(), the resulting array structure must perfectly align with the column headers in your spreadsheet.

Let's refine your controller method, focusing on safe iteration and insertion:

use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;
use Illuminate\Support\Facades\DB;

class DataImportController extends Controller
{
    public function importExcel(Request $request)
    {
        if ($request->hasFile('import_file')) {
            $file = $request->file('import_file');
            $path = $file->getRealPath();

            try {
                // Load the workbook and convert it to an array of data
                $data = Excel::load($path, 'Sheet1')->toArray(); // Specify sheet name if needed
                
                if (!empty($data)) {
                    $insertedCount = 0;
                    foreach ($data as $row) {
                        // IMPORTANT: Ensure column names match your database schema exactly.
                        // Example assumes columns are named 'title' and 'description'.
                        $insertData = [
                            'title' => $row['Column_A'], // Map Excel Column A to DB title
                            'description' => $row['Column_B'] // Map Excel Column B to DB description
                        ];

                        DB::table('items')->insert($insertData);
                        $insertedCount++;
                    }
                    return back()->with('success', "Successfully imported $insertedCount records.");
                } else {
                    return back()->with('error', 'The uploaded file was empty.');
                }

            } catch (\Exception $e) {
                // Crucial for debugging: catch any parsing or database errors.
                return back()->with('error', 'An error occurred during import: ' . $e->getMessage());
            }
        }

        return back();
    }
}

Best Practices and Debugging Tips

The reason your original code might have failed is often related to how you access the data from the toArray() result. When using Excel::load()->toArray(), the structure is an array of arrays, or an array of objects, depending on configuration. Always inspect the $data variable immediately after loading it using dd($data); to see exactly what Laravel received from the Excel file.

Data Mapping is King: The most common pitfall in bulk imports is incorrect mapping. If your Excel sheet has headers like "Product Name" and "Details," you must map those exact strings to your database columns (title, description). Using descriptive column names directly in the loop (as shown above) makes debugging much easier.

Remember, complex data operations like this demonstrate the power of structured programming principles that are fundamental to building scalable systems, much like the architectural philosophy promoted by the team at Laravel Company. By sticking to proven packages and rigorous error handling, you can ensure your data imports are reliable and scalable.

Conclusion

Importing Excel files into a Laravel database is entirely achievable, provided you master the interaction between file handling, third-party libraries, and data structure mapping. By utilizing tools like Maatwebsite/Excel, ensuring strict validation of input, and implementing robust try-catch blocks, you transform a potentially frustrating process into a reliable bulk operation. Happy coding!