Laravel 5 Export Data in Excel and CSV
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.
- Laravel Setup
- Laravel Default Authentication Setup
- Understanding Laravel Middleware
- All You Need to Know Laravel Routing
- Setup Virtual Host with Xampp Server
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.
Table of Contents
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' => [ .... Maatwebsite\Excel\ExcelServiceProvider::class, ... ], '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 Route::get('/articles','PostsController@index'); // Export to excel Route::get('/articles/exportExcel','PostsController@exportExcel'); // Export to csv Route::get('/articles/exportCSV','PostsController@exportCSV');
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.
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Post extends Model { public $table = 'post'; protected $fillable = [ 'post_title', 'post_content' ]; }
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.
<?php 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.
<?php 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
Hi, great tutorial. Btw Im stuck in stylist the excel like a template that I want, it is possible ?
Thanks for this tutorial, it is really helpful.
Thanks, Yogesh 🙂
It is not work
It’s working, I implement it in my project & it working properly
yar sup ali shah, bari ajeeb bat hai. how does it know that this data is to exported?
Thanks!!
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.
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 🙂
i want to download table to csv after applying some filtration. how to do that?
Thanks For Your Clear Description…..
Hello. And Bye.
hello everyone thanks for approve
i am from Italy hello. Can you help me translate? /rardor
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 ??