Laravel 9 Import Export Excel & CSV File Example

In this tutorial will learn how to easily import and export Excel and CSV files in the Laravel 8 application while communicating with the PHP MySQL database using Maatwebsite/Laravel-Excel package.

Step 1: Install Laravel Project

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

Step 2: Configure Database Details


Step 3: Install maatwebsite/excel package

composer require maatwebsite/excel

Next go to config/app.php and paste below code

'providers' => [

'aliases' => [ 
  'Excel' => Maatwebsite\Excel\Facades\Excel::class,
php artisan vendor:publish --provider="Maatwebsite\Excel\ExcelServiceProvider" --tag=config

Step 4: Generate Fake Data and Migrate Table

php artisan migrate
php artisan tinker

After Opening the tinker, you need to run this command to generate the fake records in our database.


Step 5: Create a Routes

In this step, We will add a route to handle requests for import and export files.

use App\Http\Controllers\UserController;


Step 6: Create Import Class

php artisan make:import ImportUser --model=User

And put below code as well.


namespace App\Imports;

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

class ImportUser 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],
            'password' => bcrypt($row[2]),

Step 7: Create Export Class

php artisan make:export ExportUser --model=User

namespace App\Exports;

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

class ExportUser implements FromCollection
    * @return \Illuminate\Support\Collection
    public function collection()
        return User::select('name','email')->get();

Step 8: Create Controller

php artisan make:controller UserController

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;
use App\Imports\ImportUser;
use App\Exports\ExportUser;
use App\Models\User;

class UserController extends Controller
    public function importView(Request $request){
        return view('importFile');

    public function import(Request $request){
        Excel::import(new ImportUser, $request->file('file')->store('files'));
        return redirect()->back();

    public function exportUsers(Request $request){
        return Excel::download(new ExportUser, 'users.xlsx');

Step 9: Create Blade / View Files

<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Laravel 8 Import Export Excel & CSV File - TechvBlogs</title>
    <link rel="stylesheet" href="">

    <div class="container mt-5 text-center">
        <h2 class="mb-4">
            Laravel 8 Import Export Excel & CSV File - <a href="" target="_blank">TechvBlogs</a>
        <form action="{{ route('import') }}" method="POST" enctype="multipart/form-data">
            <div class="form-group mb-4">
                <div class="custom-file text-left">
                    <input type="file" name="file" class="custom-file-input" id="customFile">
                    <label class="custom-file-label" for="customFile">Choose file</label>
            <button class="btn btn-primary">Import Users</button>
            <a class="btn btn-success" href="{{ route('export-users') }}">Export Users</a>


Run Laravel Application

php artisan serve

After executing this command, Open in your browser.

Hi I am Amit Kumar Thakur Experienced as s Software Developer with a demonstrated history of working in the information technology and services industry. Skilled in HTML, CSS, Bootstrap4, PHP, Laravel-9 , REST API,FB API,Google API, Youtube Api, Bitbucket,Github,Linux and jQuery. Strong engineering professional focused in Computer/Information Technology Administration and Management. Currently my profile is to Software Developer, analyze the requirement, creating frame for web application, coding and maintenance.

Related Posts

Database [] not configured Laravel in Laravel ?

In this tutorial i’m going to solve Database [] not configured in laravel. The error message “Database [] not configured Laravel” indicates that there is a missing…

How to send Notifications With Database In Laravel ?

In this tutorial we’re going to learn how to send notification if someone register on our software then send him notification. Install laravel project Next to create…

Cannot access offset of type string on string

In this tutorial i have to solve this error Cannot access offset of type string on string so follow this tutorial i have solved in this tutorials….

How to Generate Instagram Graph Api Access token ?

In this tutorial im going to generate instagram access token in very easy after reading this tutorial you’ll able to generate access token. 1st step Login Facebook…

How to get a Client IP address in Laravel

In this article, We will share with you how to get an IP address in your Laravel application with several way examples. What is IP address ?…

What is the difference between TRUNCATE and DELETE ?

TRUNCATE and DELETE are both used to remove data from a table in Laravel, but they have some important differences. What is TRUNCATE ? TRUNCATE is a…

0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x