Tech Magazine

Laravel 5 Export Data in Excel and CSV

14 39,372

Get real time updates directly on you device, subscribe now.

In this article, we will discuss Laravel 5 Export Data Excel/CSV. To achieve this functionality, we are using “Maatwebsite/Laravel-Excel” package. Here I will explain you step by step to export the data to Excel or CSV as per your needs. “Maatwebsite/Laravel-Excel” package is one of a stable package for generating CSV and Excel file. This package also provides the feature to import CSV file to the database.

Before proceeding, I’m assuming you are familiar with the Basic Laravel installation, Authentication, and the MVC (Model View Controller) structure. If no then first go through the given articles which helps you to understand the basics of Laravel.

Here’s the step by step guide to export data to excel and CSV. For this setup, I’m using the product table. Which we export to excel and CSV.

Step 1: Install “Maatwebsite/Laravel-Excel” package

We have to install the package using given composer command.

composer require maatwebsite/excel

You can also use second way to add this package to your project. Add the given code in your composer JSON and run composer update command to install the package.

require: {
  "maatwebsite/excel": "^3.0"

After that, you have to update your config/app.php and add provider and aliases array.

'providers' => [

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

To publish the package config, run the vendor publish command:

php artisan vendor:publish

This command shows you the available packages. Then you can select the package Maatwebsite to publish the config. This will create a new config file named config/excel.php.

Step 2: Register Route

Now, add given routes to your routes/web.php.

// List all alrticle

// Export to excel

// Export to csv

Step 3: Create Post Model

Use gave artisan command to create post model.

php artisan make:model Post

After that add fillable in your model.

namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
  public $table = 'post';
  protected $fillable = [

Step 4: Create a custom export class

Create a new class called PostExport in App/Exports. In this class, we will handle the logic behind export functionality.

namespace App\Exports;
use App\Post;
use Maatwebsite\Excel\Concerns\FromCollection;

class PostExport implements FromCollection
  public function collection()
    return Post::all();

In this class, create collection function which one returns an array of all the articles.

Step 5: Create Post Controller

Use given artisan command to create post controller.

php artisan make:controller PostsController

After that create three methods as per the registered routes.


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;
use App\Post;
use App\Exports\ListExport;

class PostsController extends Controller
   * Get list of articles
  public function index()
    $list = Post::get();
    return view('list',compact('list'));

   * Export to excel
  public function exportExcel()
    return Excel::download(new ListExport, 'list.xlsx');

   * Export to csv
  public function exportCSV()
    return Excel::download(new ListExport, 'list.csv');

Finally, Basic export functionality is successfully done. As I already mentioned package provides rich features for import and export data. We will discuss more on this topic. Feel free to comment if any query.

If you like our content, please consider buying us a coffee.
Thank you for your support!
Buy Me a Coffee

Get real time updates directly on you device, subscribe now.

  1. Zameer says

    Hi, great tutorial. Btw Im stuck in stylist the excel like a template that I want, it is possible ?

  2. Yogesh singh says

    Thanks for this tutorial, it is really helpful.

    1. Code Briefly says

      Thanks, Yogesh 🙂

      1. Vinay Singh says

        It is not work

        1. Rishabh Srivastava says

          It’s working, I implement it in my project & it working properly

  3. Qasim Ali says

    yar sup ali shah, bari ajeeb bat hai. how does it know that this data is to exported?

  4. Jayshree says

    I have used your solution. It’s working but I am facing one issue. While downloading CSV file first column first cell show double code.How to fix this issue. Please give some solution. I have tried lots of solutions but still not fixed that issue.
    Thanks in advance.

    1. Code Briefly says

      Hi Jayshree
      It’s hard to regenerate the same issue. Please recheck your code once, then try to debug the same. If still not tracing the issue then share your code with me.
      You can share via bitbucket, Github. Thanks 🙂

  5. sruthi says

    i want to download table to csv after applying some filtration. how to do that?

  6. Khan says

    Thanks For Your Clear Description…..

  7. rardgache says

    Hello. And Bye.

  8. rardgache says

    hello everyone thanks for approve

  9. rardgache says

    i am from Italy hello. Can you help me translate? /rardor

  10. Rahul says

    how to current page record download in excel sheet any filtered record display on this table click excel sheet download button this current page data downloaded in excel sheet ??

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. AcceptRead More