laravel excel import Export to Database Example

Today, We want to share with you laravel excel import Export to Database.In this post we will show you Laravel 7 Import Export Excelsheet to database Example, hear for Laravel Export and Import Excel using maatwebsite package we will give you demo and example for implement.In this post, we will learn about Laravel 7 maatwebsite import excelsheet into DB and export data into csv and excelsheet with an example.

laravel excel import Export to Database

There are the Following The simple About maatwebsiteexcel::download from array Full Information With Example and source code.

As I will cover this Post with live Working example to develop laravel excels::import skip first row, so the how to import and export in laravel is used for this example is following below.

Phase 1 : Install Laravel Web Project

install Laravel application

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

Phase 2: Install Maatwebsite Package

install Maatwebsite package

composer require maatwebsite/excel

config/app.php

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

php artisan vendor:publish

Phase 3: Create Dummy Records

simply import and export.

php artisan migrate
php artisan tinker

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

Phase 4: Add Routes

routes/web.php

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

Phase 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('123456'),
        ]);
    }
}

Phase 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();
    }
}

Phase 7: Create 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 importExportView()
    {
       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();
    }
}

Phase 8: Create Blade File

resources/views/import.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Laravel 5/6/7 Import Export Excelsheet to database Example - 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="cart card bg-light mt-3">
        <div class="card-header">
            Laravel 5/6/7 Import Export Excelsheet to database Example - Pakainfo.com
        </div>
        <div class="card-body">
            <form action="{{ route('import') }}" method="POST" enctype="multipart/form-data">
                @csrf
                <input type="file" name="file" class="dsp form-control jdk">
                <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>

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 laravel7 excel::import return.
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.


Related FAQ

Here are some more FAQ related to this Article:

  1.   Number Filter using Angular Example
  2.   Laravel 6 Update Query using Eloquent Example
  3.   Angular UI Grid Table Tutorial With Example
  4.   Laravel 5.8 Features and Updates
  5.   AngularJS Remove Last Comma from String
  6.   Laravel 6 Eloquent Has With and WhereHas
  7.   Laravel 5/6/7 Import & Export data to Excel CSV using maatwebsite
  8.   Database Data Import to Excel File using laravel
  9.   How to Send Emails in Laravel?
  10.   Add Session Array programming with php