Get Excel, PDF, and CSV Downloads with Laravel Excel

 How to Download Excel,PDF,CSV files using Laravel Excel in Laravel

Download Excel,PDF,CSV with Laravel Excel

In this article we will learn how to download your data in Excel , PDF , CSV and in other formats in Laravel . We will use Laravel Excel package which is also known as maatwebsite package . Let's see how to use this package with example .

Step 1 - Install Laravel Excel Package :

Use the following command to install Laravel Excel Package on your project 

composer require maatwebsite/excel

Now we need to register the service provider inside config/app.php :

Add the following class in the providers array inside config/app.php .

'providers' => [


Add the following class in the aliases array inside config/app.php .

'aliases' => [

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

Now publish your config changes using the following command .

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

Step 2 - Setup Your Route :


Step 3 - Create Export File :

For downloading files in Excel , PDF , CSV or in any format using Laravel Excel we have to create a Export file for writting our Database Query and we will call this export file in our controller .

Use the following command and create a export file .

php artisan make:export filename --model=Model_name

php artisan make:export UsersExport --model=User

It will create a your file inside app/Exports/UsersExport.php . Now open this file and write your Database Query inside the collection method .

app/Exports/UsersExport.php :


namespace App\Exports;

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

class UsersExport implements FromCollection
    * @return \Illuminate\Support\Collection
    public function collection()
        return User::all();

Step 4 - Setup Your Controller :

Now everything is done , we just need call the export file in our controller method in order to download the file as shown below .


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Exports\UsersExport;
use Maatwebsite\Excel\Facades\Excel;

class LaraExcelController extends Controller
    public function download()
    	//To download file in Excel Format
    	return Excel::download(new UsersExport, 'users.xlsx');

To Download in PDF format :

return Excel::download(new UsersExport, 'users.pdf');

To Download in CSV format :

return Excel::download(new UsersExport, 'users.csv');

Note :

While Downloading your file using this package for the first time you might get the following error .

Unable to resolve NULL driver for [Maatwebsite\Excel\Transactions\TransactionManager]

To resolve this issue please run any of the following command .

php artisan config:cache

php artisan config:clear

Previous Post Next Post

Contact Form