Export Data to Excel in Laravel Example

Today, We want to share with you laravel excel export example.In this post we will show you import csv or excel file and export csv or excel file using maatwebsite/excel version 3 composer package, hear for how to export import Excel spreadsheet or csv file to database in php laravel framework. we will give you demo and example for implement.In this post, we will learn about using maatwebsite/excel laravel with an example.

Laravel Import Export Excel to database Example

The simple way to start an export is to make a custom export class. We’ll use members an invoices export as example. how to comfortably import export excel or CSV file to Database file from the database with the maatwebsite/excel composer package in Laravel Application.

Follow Bellow Few Step:

  • 1) Install Required Packages
  • 2) Configuration app.php file
  • 3) Create route
  • 4) Create controller
  • 5) Create view file

Database Data Import to Excel File using laravel
Database Data Import to Excel File using laravel1

simple way put composer.json this code
and then run this commands “composer update”.
"require": {
    "php": ">=5.6.4",
    "laravel/framework": "5.4.*",
    "laravel/tinker": "~1.0",
    "maatwebsite/excel": "~2.1.0"
},

Step 1 : Install Laravel Application

composer create-project --prefer-dist laravel/laravel member_shop

Step 2: Install Maatwebsite Package

In second methos for install any package it very easy. open terminal and run bellow command then you can install your any required package.

composer require maatwebsite/excel

config/app.php

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

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

and then simple you can make publish configuration file by using following command:

php artisan vendor:publish

and then here create a new config file named “config/excel.php”.

Read Also:  Laravel Eloquent limit() Query Example

Step 3: Create Dummy Records

php artisan migrate

php artisan tinker

factory(App\Member::class, 50)->create();

Step 4: Add Routes

routes/web.php

Route::get('export', '[email protected]')->name('export');
Route::get('membersImpExpList', '[email protected]');
Route::post('import', '[email protected]')->name('import');

Step 5: Create Import Class

php artisan make:import MembersImport --model=Member

app/Imports/MembersImport.php

<?php
  
namespace App\Imports;
  
use App\Member;
use Maatwebsite\Excel\Concerns\ToModel;
  
class MembersImport implements ToModel
{
    /**
    * @param array $row
    *
    * @return \Illuminate\Database\Eloquent\Model|null
    */
    public function model(array $row)
    {
        return new Member([
            'name'     => $row[0],
            'email'    => $row[1], 
            'password' => \Hash::make('98256'),
        ]);
    }
}

Step 6: Make Export Class

php artisan make:export MembersExport --model=Member

app/Exports/MembersExport.php

<?php
  
namespace App\Exports;
  
use App\Member;
use Maatwebsite\Excel\Concerns\FromCollection;
  
class MembersExport implements FromCollection
{
    /**
    * @return \Illuminate\Support\Collection
    */
    public function collection()
    {
        return Member::all();
    }
}

Step 7: Create a Laravel Controller

app/Http/Controllers/MemberController.php

<?php
   
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\Exports\MembersExport;
use App\Imports\MembersImport;
use Maatwebsite\Excel\Facades\Excel;
  
class MemberController extends Controller
{
    /**
    * @return \Illuminate\Support\Collection
    */
    public function membersImpExpList()
    {
       return view('import');
    }
   
    /**
    * @return \Illuminate\Support\Collection
    */
    public function export() 
    {
        return Excel::download(new MembersExport, 'members.xlsx');
    }
   
    /**
    * @return \Illuminate\Support\Collection
    */
    public function import() 
    {
        Excel::import(new MembersImport,request()->file('file'));
           
        return back();
    }
}

Step 8: Create Blade File

resources/views/import.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Laravel PHP Import Export Excel to database Example - www.pakainfo.com</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" />
</head>
<body>
   
<div class="container">
    <div class="card bg-light mt-3">
        <div class="card-header">
            Laravel PHP Import Export Excel to database Example - www.pakainfo.com
        </div>
        <div class="card-body">
            <form action="{{ route('import') }}" method="POST" enctype="multipart/form-data">
                @csrf
                <input type="file" name="file" class="form-control">
                <br>
                <button class="btn btn-success">Import Member Data</button>
                <a class="btn btn-warning" href="{{ route('export') }}">Export Member Data</a>
            </form>
        </div>
    </div>
</div>
   
</body>
</html>

I hope you get an idea about Laravel Import Export Excel & CSV File Tutorial with Example.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.