Import and Export CSV File using Laravel 6 and MySQL

Today, We want to share with you Import and Export CSV File using Laravel 6 and MySQL.In this post we will show you Import Export Excel & Csv to Database in Laravel 6 Example, hear for laravel 6.0 import csv to database we will give you demo and example for implement.In this post, we will learn about Import and Export CSV file in Laravel 6 with an example.

Import and Export CSV File using Laravel 6 and MySQL

There are the Following The simple About How to Import Export CSV File Data in Laravel 6 Full Information With Example and source code.

As I will cover this Post with live Working example to develop Laravel 6.0 Export Data in Excel and CSV, so the Export or Import of CSV or Excel file in Laravel 6.0 with MySQL is used for this example is following below.

Step 1 : Install Fresh Laravel 6.0 Web Application

run bellow command into install Laravel 6.0 application

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

Step 2: Install Maatwebsite Package

Composer package manager

composer require maatwebsite/excel

config/app.php

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

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

Step 3: Added Dummy Records in Database

php artisan migrate

generate dummy users

php artisan tinker
factory(App\Member::class, 20)->create();

Step 4: Define a Laravel 6 Routes

routes/web.php

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

Step 5: Laravel 6 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;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
    
class MembersImport implements ToModel, WithHeadingRow
{
    /**
    * @param array $row
    *
    * @return \Illuminate\Database\Eloquent\Model|null
    */
    public function model(array $row)
    {
        return new Member([
            'name'     => $row['name'],
            'email'    => $row['email'], 
            'password' => \Hash::make($row['password']),
        ]);
    }
}

You can simple download here demo for csv file from here: Demo CSV File.

Also Read This πŸ‘‰   Laravel Eloquent Relationships Join Multiple Tables

Step 6: Laravel 6 Create 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 Controller in Laravel 6

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 MemberIMPExpData()
    {
       return view('import');
    }
   
    /**
    * @return \Illuminate\Support\Collection
    */
    public function export() 
    {
        return Excel::download(new MembersExport, 'users.xlsx');
    }
   
    /**
    * @return \Illuminate\Support\Collection
    */
    public function import() 
    {
        Excel::import(new MembersImport,request()->file('file'));
           
        return back();
    }
}

Step 8: Laravel 6 Create Blade File

resources/views/import.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>PHP Laravel 6.0 Import Export Excel to MySQL database Tutorials - jio - tamilrokers</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">
            PHP Laravel 6.0 Import Export Excel to MySQL database Tutorials - jio - tamilrokers
        </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>
	<h3>Sponser Ship:</h3>
	<ul>
	<li>bachelor 2020</li>
	<li>The Bachelor 2020</li>
	<li>Bachelor</li>
	<li>Hannah Brown</li>
	<li>Peter Weber</li>
	<li>Reality Steve</li>
	<li>reality steve bachelor peter weber</li>
	<li>the bachelor season 24 episode 1</li>
	<li>peter bachelor</li>
	<li>who does peter pick on the bachelor</li>
	</ul>
</div>
   
</body>
</html>

Web Programming Tutorials Example with Demo

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about how to export data in excel file in laravel.
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.