Laravel 9 Import Export CSV/EXCEL File Example

Websolutionstuff | Feb-19-2022 | Categories : Laravel PHP

In this tutorial, I will give you laravel 9 import export csv and excel file example. We will simply create import data to xls, csv file and also we will import data to the database using csv file in laravel 9.

Using the maatwebsite/excel plugin you can easily import and export csv and excel files in laravel 9 examples. For laravel 9 import csv/excel file use import class, laravel 9 export csv/excel file use export class.

Using this example we can easily import, export, and download the csv & excel file from the database using the maatwebsite/excel composer package.  maatwebsite/excel provide an easy way to import and export csv files in laravel 9 using the database model.

So let's see how to import csv and excel files in laravel 9, import/export csv and excel in laravel 9, download csv and excel file in laravel 9.

Step 1 : Create a new project in laravel 9

We are creating a new project set up for this example, So create a new project using the below command.

composer create-project --prefer-dist laravel/laravel import_export



Step 2 : Database Setup

In the second step, we will configure the database like database name, username, password, etc. So, open the .env file and fill in all details like as bellow: 

DB_DATABASE=database name
DB_USERNAME=database username
DB_PASSWORD=database password


Step 3 : Install Package

Now, we are going to install the Maatwebsite package using the below command.

composer require maatwebsite/excel


Step 4 : Create Some Dummy Records Using Tinker

After adding aliases and providers are adding some dummy records in the databse using the below command.

php artisan tinker

factory(App\User::class, 100)->create();



Step 5 : Create New Route

Now in this step, we are creating a new route for this example in this path routes/web.php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Import_Export_Controller;
| Web Routes
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
    Route::get('import_export', 'importExport');
    Route::post('import', 'import')->name('import');
    Route::get('export', 'export')->name('export');


Step 6 : Add Controller

Create the Import_Export_Controller using the following command.

php artisan make:controller Import_Export_Controller

After running this command we need to add the below code in this controller.


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Exports\ExportUsers;
use App\Imports\ImportUsers;
use Maatwebsite\Excel\Facades\Excel;

class Import_Export_Controller extends Controller
     public function importExport()
       return view('import');

    public function export() 
        return Excel::download(new ExportUsers, 'users.xlsx');

    public function import() 
        Excel::import(new ImportUsers, request()->file('file'));
        return back();



Step 7 : Create Import Class

Now, we will create the import class using the below command.

php artisan make:import ImportUsers --model=User

After running this command you will find ImportUsers.php in this path app\Imports\ImportUsers.php add the below code in this file.


namespace App\Imports;

use App\User;
use Maatwebsite\Excel\Concerns\ToModel;

class ImportUsers implements ToModel
    * @param array $row
    * @return \Illuminate\Database\Eloquent\Model|null
    public function model(array $row)
        return new User([
            'name'     => $row[0],
            'email'    => $row[1],


Step 8 : Create Export Class

Now, we will create the export class using the below command.

php artisan make:export ExportUsers --model=User

After running this command you will find ExportUsers.php in this path app\Export\ExportUsers.php add the below code in this file.


namespace App\Exports;

use App\User;
use Maatwebsite\Excel\Concerns\FromCollection;

class ExportUsers implements FromCollection
    * @return \Illuminate\Support\Collection

    public function collection()
        return User::all();



Step 9 : Create Blade File for View

Now create import.blade.php file for view in this path resources/views/import.blade.php

<!DOCTYPE html>
<html lang="en">
  <title>Laravel 9 Import Export CSV/EXCEL File Example - Websolutionstuff</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="">
  <script src=""></script>
  <script src=""></script>
<div class="container">
	<h3>Laravel 9 Import Export CSV/EXCEL File Example - Websolutionstuff</h3>
	<form action="{{ route('import') }}" method="POST" name="importform"
		<div class="form-group">
			<label for="file">File:</label>
			<input id="file" type="file" name="file" class="form-control">
		<div class="form-group">
			<a class="btn btn-info" href="{{ route('export') }}">Export File</a>
		<button class="btn btn-success">Import File</button>

Copy below command and run in terminal.

php artisan serve

And finally, you can run this project on your browser. 



You might also like :

Recommended Post
Featured Post
Paypal Payment Gateway Integration In Laravel 8
Paypal Payment Gateway Integra...

In this tutorial, we will see paypal payment gateway integration in laravel 8. Paypal is an international payment p...

Read More


How To Convert HTML To PDF using JavaScript
How To Convert HTML To PDF usi...

In this example we will see how to convert html to pdf using javaScript. PDF file format is very useful to dow...

Read More


Multiple Row Grouping Datatables jQuery
Multiple Row Grouping Datatabl...

In this article, we will see how to row group in datatable in jquery. Datatables doesn't have row grouping buil...

Read More


How To Add Toastr Notification In Laravel
How To Add Toastr Notification...

In this tutorial, I will show you how to add toastr notification in laravel application. There are many types of no...

Read More