How to export data in Excel using Laravel

In this tutorial we will learn how to export  dynamic data in excel file with heading using laravel.

 If you do not know how to install laravel then click here to install laravel .

Step 1- Install Laravel Maatwebsite Excel

Open your CMD and just run this command to install this package.

composer require maatwebsite/excel

Step 2- Now,  go to config\app.php

Link installed package in app.php file

'providers' => [




'aliases' => [


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


Step 3- Run below command for publish the vendor file

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

Step 4 – Create  'users' table in database


User table data


Step 5- Create model for User 

php artisan make:model User

Then, run this command in CMD to generate UserExport.php file

php artisan make:export UserExport --model=User


Now this file has been created in the app folder, Generated file path is app/export/UserExport.php.

This is our UserExport.php file



namespace App\Exports;


use App\User;

use Maatwebsite\Excel\Concerns\FromCollection;

use Maatwebsite\Excel\Concerns\WithHeadings;


class UserExport implements FromCollection ,WithHeadings



    * @return \Illuminate\Support\Collection


    public function collection()


        return User::select('name''email''age')->get();


    public function headings(): array


        return [

            'Name''Email ID','Age'




step 6 – Create route 


Step 7 – create   controller  

php artisan make:controller UserController




namespace App\Http\Controllers;


use App\Exports\UserExport;


class UserController extends Controller


    Function export(){

        return Excel::download(new UserExport(), 'users.xlsx');




Step 8 – run php artisan serve


Then, go to your browser and simply run the below url



Here users.xlsx file downloaded



Related Topics



Road map for web development 2022 What is git and git hub How to create a Google logo using HTML and CSS What is .htaccess file and how to use it Most important HTML5 interview questions PHP CRUD operations How to send mail using PHP Insert and fetch image using PHP How to dowload pdf using PHP How to export data in Excel using Laravel Import excel data in database using laravel How to send mail in Laravel 8 Laravel 8 CRUD Operation Laravel 8 Image Upload and Fetch Tutorial Example Laravel 8 CRUD operation using DB Laravel 8 CRUD Operation using resource controller Date Validations Using jquery Datepicker How to reset or clear a form using JavaScript Form validation in Laravel 8 How to download pdf file using Laravel 8 dompdf How to Toggle Password Visibility Using JavaScript PHP CRUD Operation Using Ajax and Jquery Get Yesterday and Tomorrow Date in PHP How to disable text selection using CSS and Jquery How to Get Previous Month and Next month date from Date in PHP Laravel CRUD Operation Using Ajax and Jquery How to find numbers from string in PHP How to Convert String Date to Date Format in PHP How to calculate age from dob column using mysql Rotate image using JQuery animate() method Allow Only Numbers in a Text Box using jQuery How To connect SSH using PHP? Radio buttons value using GET method in PHP How to get X and Y position on click event using jQuery Delete Confirmation Message using JavaScript Select2 DropDown With Validation using Bootstrap 4 How to Zoom an Image on Mouse Hover using CSS How to Remove First Element from Array in PHP How to Remove Last Element from Array in PHP E-Signature Pad using jQuery, Ajax and PHP PHP Contact Form with Google reCAPTCHA How to update profile image in Laravel using Ajax How to get X and Y position of an element using jQuery How to disable a anchor tag in HTML using CSS, JS and jQuery check if atleast one checkbox is checked or not using jQuery How can we count number of rows in a table using jQuery How can we count child elements inside a div using jQuery How to use radio button on change event in jQuery How to get checked radio button value by name using jQuery How to count the number of character in a string using jQuery How to add modal popup in PHP using jQuery?