Laravel 7 Export CSV And Excel From From Database




In this article, you will learn how to export Excel or CSV data in the file from a database into the laravel web application using the maatwebsite package with an example.

When you work with any Laravel web application. So you may need to get data from MySQL database in laravel web application with many different formats like CSV and Excel.

In this tutorial, we will show in an easy and simple way how data can be obtained in Excel and CSV files from MySQL database in Laravel web applications.

Laravel Export CSV and Excel From Database Example Tutorial

  • Install Laravel Application
  • Configure Database Detail
  • Run Migration
  • Install Maatwebsite Package
  • Generate Fake Data in Table
  • Create Routes
  • Generate Controller By Artisan
  • Generate Export Class by Artisan
  • Create Blade View
  • Start Development Server

Install Laravel Application

First Step, you need to download or install laravel web application new setup on your local machine. you can use the following command to download or install laravel application new setup:

composer create-project laravel/laravel laravel-export –-prefer-dist

If you want to install a specific version of laravel, you can follow the following tutorial

Configure Database Detail

After that, you will set up the database credentials in .env file. So open your .env file and update database credentials following:


Run Migration

Before you will run php artisan migrate command. Go to app/providers/AppServiceProvider.php and update the below code :

use Illuminate\Support\Facades\Schema;
function boot()

Next, go to command prompt and run the following command:

php artisan migrate


php artisan migrate:fresh

This php artisan migrate command creates a table in your database.

Install Maatwebsite Package

Next, you need to Install Maatwebsite Package in your laravel web applications. So go to your command prompt and run the following command to install the latest laravel Install Maatwebsite Package:

composer require maatwebsite/excel

When the package of Maatwebsite Package is installed. After that this package will have to be registered in the Laravel web application of the app. For this, you can register this package of instruction follow below.


 'providers' => [


 'aliases' => [

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


When you will register the package of Maatwesite. After that, the package will have to be published with the command following command.

php artisan vendor:publish

Generate Fake data in Table

Next, we need to generate fake data into the MySQL database table by using php artisan command. So go to your command prompt (cli) and then go to your project root directory. After that run the following command:

php artisan tinker

After run the php artisan tinker. Use the following command. This command will add 200 fake data in your MySQL database table users:

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

Create Routes

Next step, go to routes/web.php file and update the following routes into your file:

Route::get('export-sample', '[email protected]');
Route::get('export', '[email protected]');

Generate Export Class by Artisan

Here, you can create an export class by using the following PHP artisan command. So go to your command prompt type the following command:

php artisan make:export ExportUsers --model=User

After that, go to app/Exports/ExportUsers.php and your created export class looks like:

namespace App\Exports;
use App\User;

use Maatwebsite\Excel\Concerns\FromCollection;
class ExportUsers implements FromCollection
    * @return \Illuminate\Support\Collection
    public function collection()
        return User::get();

Generate Controller By Artisan

Now, you need to generate or create a controller file using php artisan command, so use the php artisan make:controller command and generate/create a new controller file:

php artisan make:controller ExportController

In this controller file, we will create some new methods. This method will also show export button and export data into mysql database..

After that, go to App/Http/Controllers/ directory and find the created ExportController file and update the following code into your file:

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Exports\ExportUsers;
use Maatwebsite\Excel\Facades\Excel;
class ExportController extends Controller
    * @return \Illuminate\Support\Collection
    public function index()
       return view('export');
    * @return \Illuminate\Support\Collection
    public function export() 
        return Excel::download(new ExportUsers, 'users.xlsx');


Create Blade Views

Next, you need to create a blade views file, so go resource/views directory and create the following blade views file.

Go to resource/views/ then create a new file name export.blade.php and update the following code:

<!DOCTYPE html>
    <title>Laravel Export Excel to Database -</title>
    <link rel="stylesheet" href="" />
<div class="container">
    <div class="card mt-4">
        <div class="card-header">
            Laravel 7 Export Excel to Database -
        <div class="card-body">
          <a class="btn btn-info" href="{{ url('export') }}">Export File</a>


Start development server

You can use the following command artisan command to start the development server:

php artisan serve

The final step, go to your browser and type the below-given URL into your browser:




You can also download laravel export excel or CSV web application here Download


This tutorial idea has taken from this