How to Import Excel Files in Laravel 9?

Last updated on December 29, 2022

Importing excel file data into the Laravel framework is easy with the maatwebsite/excel package. In the previous article, we learned about exporting data in excel files. In this article, we will learn how to import excel files to the Laravel framework.

If you haven’t installed the Laravel framework yet, you can check our article which helps you to install Laravel fresh applications on your local web server.

Next, install the maatwebsite/excel package in your Laravel application. Run the below command in your terminal to install maatwebsite/excel package

composer require maatwebsite/excel

After installation, you need to do a few configurations in Laravel. Open app/config.php and add the following class into the providers array,

Maatwebsite\Excel\ExcelServiceProvider::class,

Then register the following facade

'Excel' => Maatwebsite\Excel\Facades\Excel::class,

After the above steps, run the below command to publish the vendors.

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

Import Excel File to Laravel

After installation and configuration run the below command that creates the UsersImport class into the Imports folder.

php artisan make:import UsersImport --model=User

UsersImport class comes with a default method model(), in which you can define table columns names that need to be filled from excel sheets.

public function model(array $row)
{
    return new User([
        'name'     => $row[0],
        'email'    => $row[1],
        'password' => \Hash::make('secret'),
    ]);
}

In the above model() method name and email columns, data come from the imported excel sheet, and the password is simply generated through the make() method.

Next, import excel facade and UsersImport class into UserController

use App\Imports\UsersImport;
use Maatwebsite\Excel\Facades\Excel;

After importing the facade and UsersImport class, create an import method in the UserController class that will import the excel file from incoming requests.

public function import()
{
    Excel::import(new UsersImport,request()->file('excel_file'));
        
    return back();
}

Next, create a blade file (form.blade.php) in the resources/views directory and add an HTML form that will post the excel file to the import method through route.

<form action="{{ route('import') }}" method="post" enctype="multipart/form-data">
    @csrf
    <input type="file" name="excel_file" />
    <input type="submit" />
</form>

After creating the blade file, we need a method that should load the form from view. So create a form() method in UserController class.

public function form()
{
    return view('form');
}

Now, we have two methods import() and form() in the UserController class. Next, we need routes to access both methods.

Open web.php from the routes directory and add the below routes.

use App\Http\Controllers\UserController;

Route::get('/form', [UserController::class, 'form']);
Route::post('/import', [UserController::class, 'import'])->name('import');

The import feature is almost ready to test. Next, open the terminal and run the below command that serves the Laravel application on the development server

php artisan serve

Once the command runs you can test the import feature on the following URL,

http://127.0.0.1:8000/form

Add an excel file to the form and submit then check your users’ table.

Conclusion

In this article, we learned how to import an excel file into the Laravel framework using maatwebsite/excel package. If you would like to learn more about it check our Laravel page.


Written by
I am a skilled full-stack developer with extensive experience in creating and deploying large and small-scale applications. My expertise spans front-end and back-end technologies, along with database management and server-side programming.

Share on:

Related Posts

Tags: Laravel,