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

How to Crawl any website Meta Title and Meta Description in Laravel ?

1st step install below package. Next to create Controller First go to route and put below code Next go to controller and put below code Next go…

SQLSTATE[HY000] [2002] No such file or directory (Connection: mysql, SQL: insert into `oauth_clients` (`user_id`, `name`, `secret`

In this tutorial i’m going to solve the error SQLSTATE[HY000] [2002] No such file or directory (Connection: mysql, SQL: insert into oauth_clients (user_id, name, secret Error :-…

Top 20 Laravel Interview Question in 2024

In this tutorial im going share interview experience For laravel developer. A list of top frequently asked Laravel Interview Questions and answers are given below. Q #1) What is…

How to Get Google Analytics API key ?

In this tutorial we’re going to share how to get the google Analytics API key. I have shared in very easy way. First go enable Google analytics…

Youtube Subscriber Count in ReactJs

In this tutorial i’m going to learn how to count YouTube Subsribers count, views count as well, as define below. In order to install your app, first…

How to Disable Laravel’s Eloquent timestamps in laravel ?

In this tutorial we’re going to share how to disable the automatic created_at and updated_at timestamps in Laravel’s Eloquent models, along with explanations of different scenarios: 1st…

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