skip to Main Content
Laravel Export Table Data To Excel Sheet Using Maat Website Package

Laravel export table data to excel sheet using Maat website package

Many times we need to export Users, Products, Orders etc… to the Excel file. Using Laravel Maat Website package we can easily fetch data from database using eloquent model then export database record to excel file using Laravel Maat website package. Let’s see Laravel export table data to excel sheet using Maat website package.

Today we are learning the export user data to excel file. For exporting user data to excel file, first of all, we are getting the user data using eloquent model and export the excel file.

First, we need to install Laravel Maat website package for export data to excel file.

I hope you installed the Laravel if not follow this tutorial.

First of all, we need to install the Excel library on Laravel.

Let’s open the composer.json file located at the root of the Laravel.

For Laravel 5

"maatwebsite/excel": "~2.1.0"

For Laravel 4

"maatwebsite/excel": "~1.3"

After, adding the excel library let’s update the composer for downloading the new library.

composer update

Fire “composer update” command from the root of the Laravel installed.

After composer update let’s register the Excel library on the Laravel. We need to add the Service Provide and Facades entry to our Laravel setup like.

For registering the service provider let’s open the “config/app.php” file of the Laravel and add service provider.


Let’s register facade on the same file.

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

Awesome, We register the Service Provider and Facades on the Laravel. Now, Let’s generate the config file for the Excel Library.

For Laravel 5

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

The config files can now be found at config/excel.php

For Laravel 4

php artisan config:publish maatwebsite/excel

The config files can now be found at app/config/packages/maatwebsite/excel

The Excel Library installation process is finished. Now let’s start the learning how to export user data to excel file format.

Let’s create the `exportUser` method to your UserController like

Laravel export table data to excel sheet


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;

class UserController extends Controller

    public function getBladeExlce()
        $userData = User::all();

        \Excel::create('thecodingstuff', function($excel) use($userData) {

            $excel->sheet('user', function($sheet) use($userData) {

                $excelData = [];
                $excelData[] = [
                    'First Name',
                    'Last Name',

                foreach ($userData as $key => $value) {
                    $excelData[] = [

                $sheet->fromArray($excelData, null, 'A1', true, false);




So Let’s see it’s too easy to export user data to excel file.

If you have any question or face any issue write the comment below.


I am a web designer and developer part-time blogger. Write something on some free time. Find my every day thought here :)

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top