Laravel 5.6 Data Export to CSV and Excel Example

Hello coders, In this tutorial, I will show you how to do Laravel 5.6 Data Export to CSV and Excel. In this example, we use Laravel Excel 3.0 and Laravel 5.6. The maatwebsite package is the most popular package to export CSV file and excel file. One of the most important changes about a maatwebsite package 3.0 is that it has deprecated some of the version 2 packages’ methods and also version 2 is not compatible with Laravel 5.6. So this example’s method to generate CSV or Excel is quite different from the previous package’s methods. For more information, you can follow this link.

Upgrading to 3.0 from 2.*

Version 3.0 will not be backward compatible with 2.*. It’s not possible to provide a migration guide.

New dependencies

  • Requires PHP 7.0 or higher.
  • Requires Laravel 5.5 (or higher).
  • Requires PhpSpreadsheet instead of PHPExcel.

Deprecations

ALL Laravel Excel 2.* methods are deprecated and will not be able to use in 3.0.

  • Excel::load() is removed and will not be re-added until 3.1
  • Excel::create() is removed and replaced by Excel::download/Excel::store($yourExport);

Laravel 5.6 Data Export to CSV and Excel Example

We are going to Configure Laravel Project.

#1: Download Laravel Project

Install New Laravel Project by the writing following command.

$ composer create-project --prefer-dist laravel/laravel exportdata

#2: Configure SQL Database

Now we can setup database credentials.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=exportdata
DB_USERNAME=root
DB_PASSWORD=

#3: Install maatwebsite Package

Now we will install maatwebsite/excel Package in our project.

composer require maatwebsite/excel

#4: Define providers and aliases

Find the providers in config >> app.php file and register the ExcelServiceProvider.

'providers' => [
                 Maatwebsite\Excel\ExcelServiceProvider::class,
               ],

Find the aliases in config >> app.php file and register that.

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

#5: Publish Config

To publish the config, run the vendor publish command:

php artisan vendor:publish

#6: Create a Model and Migration

Type the following command in your terminal.

 php artisan make:model Product -m

It will create two files.

  1. Product.php model.
  2. create__products_table migration file.
//create_products_table
 
 public function up()
    {
        Schema::create('products', function (Blueprint $table) {
            $table->increments('id');
            $table->string('productname');
            $table->integer('price');
            $table->timestamps();
        });
    }

#7: Create a view file

Create a file in resources  >>  views  >>   product.blade.php and put this following code in it.

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Laravel Export To Csv and Excel</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"/>
  </head>
  <body>
    <div class="container">
    </br>
      <div class="row">
        <div class="col-md-4"></div>
        <div class="col-md-2">
          <form action="{{url('product/export')}}" enctype="multipart/form-data">
            <button class="btn btn-success" type="submit">Export</button>
          </form>
        </div>
      </div>
    </div>
  </body>
</html>

#8: Create one controller and route 

php artisan make:controller ProductController

It will create a controller file called ProductController.php.

We register route in a web.php file.

Route::get('product', 'ProductController@create');
Route::get('product/export', 'ProductController@exportFile');

Add code to create() function to display view.

//ProductController.php

public function create()
    {
        return view('product');
    }

Laravel Data Export to CSV and Excel Example

#9: Write an export function in ProductController.

The simplest way to start an export is to create a custom export class. We will use a DataExport as an example.

Create a new class called DataExport in App/Exports:

//DataExport.php

<?php

namespace App\Exports;
use App\Product;

use Maatwebsite\Excel\Concerns\FromCollection;

class DataExport implements FromCollection
{
    public function collection()
    {
        return Product::all();
    }
}

In ProductController.php we can now download this export.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Excel;
use App\Product;
use App\Exports\DataExport;


class ProductController extends Controller
{
    public function create()
    {
        return view('product');
    }
        public function exportFile()
        {
         return Excel::download(new DataExport, 'data.xlsx');
        }
  
}

Laravel 5.6 Data Export Example

I have written the whole file, and finally our Laravel 5.6 Data Export to CSV and Excel Example is over.

Post Your Thoughts

This site uses Akismet to reduce spam. Learn how your comment data is processed.

3 Comments