Import and Export CSV file in Laravel 7

import export excel or csv from database is a primary requirement for admin project. In this tutorial i will show you how to import csv or excel file and export csv or excel file using maatwebsite/excel version 3 in laravel 6 application.

In this export excel or csv from database tutoril we will simple create import data to csv, xls file and also we can import data to database using csv file in laravel 6 application.

In this tutorial we will use maatwebsite/excel composer package for import and export task. maatwebsite/excel provide easy way to import and export using database model. maatwebsite/excel updated version 3 and they provide great way to import export data from database, so first follow few step to get example.

Today i am coming with a new tutorial. Today i will teach you how to import and export csv file in laravel 7. In this example i will use maatwebsite/excel composer package for import and export task.

This packages is awesome to export and import csv file in laravel application. If you don't know how to upload export and import csv file in laravel application. I will teach you step by step. So don't worry.

So let's start our todays tutorial export and import csv file in laravel 7 application. 

import-and-export-csv-file-in-laravel

Step 1 : Install Laravel 6 Project

Here, we need install Laravel 6 application using bellow command, So open your terminal OR command prompt and run bellow command:

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

Step 2: Install Maatwebsite Package

In this step we need to install Maatwebsite package via the Composer package manager, so one your terminal and fire bellow command:

composer require maatwebsite/excel

Now open config/app.php file and add service provider and aliase.

config/app.php

'providers' => [

	....

	Maatwebsite\Excel\ExcelServiceProvider::class,

],

'aliases' => [

	....

	'Excel' => Maatwebsite\Excel\Facades\Excel::class,

],

Read more  : Laravel 6 Export To PDF Using Laravel Dompdf

Step 3: Add Routes

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

Step 4: Create Import Class

In maatwebsite 3 version provide way to built import class and we have to use in controller. So it would be great way to create new Import class. So you have to run following command and change following code on that file:

php artisan make:import UsersImport --model=User

app/Imports/UsersImport.php

namespace App\Imports;
   
use App\User;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
    
class UsersImport implements ToModel, WithHeadingRow
{
    public function model(array $row)
    {
        return new User([
            'name'     => $row['name'],
            'email'    => $row['email'], 
            'password' => \Hash::make($row['password']),
        ]);
    }
}

Step 5: Create Export Class

maatwebsite 3 version provide way to built export class and we have to use in controller. So it would be great way to create new Export class. So you have to run following command and change following code on that file:

php artisan make:export UsersExport --model=User
app/Exports/UsersExport.php
namespace App\Exports;
  
use App\User;
use Maatwebsite\Excel\Concerns\FromCollection;
  
class UsersExport implements FromCollection
{
   
    public function collection()
    {
        return User::all();
    }
}

Step 6: Create Controller

In this step, now we should create new controller as MyController in this path "app/Http/Controllers/DemoController.php". this controller will manage all importExportView, export and import request and return response, so put bellow content in controller file: To create this controller : run 

php artisan make:controller DemoController

app/Http/ControllersDemoController.php

namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\Exports\UsersExport;
use App\Imports\UsersImport;
use Maatwebsite\Excel\Facades\Excel;
  
class DemoController extends Controller
{
    
    public function importExportView()
    {
       return view('import');
    }
   
    public function export() 
    {
        return Excel::download(new UsersExport, 'users.xlsx');
    }
   
    public function import() 
    {
        Excel::import(new UsersImport,request()->file('file'));
           
        return redirect()->back();
    }
}

Step 7:  Create Blade File

In Last step, let's create import.blade.php(resources/views/import.blade.php) for layout and we will write design code here and put following code:

Read also : Send Notification to Inactive User with Task Scheduling in Laravel using Custom Command

resources/views/import.blade.php

Hope it will work for you. If you face any problem, themn leave a comment. 

A web enthusiastic, self-motivated Full-Stack Web Developer from Dhaka, Bangladesh with experience in developing applications using JavaScript, Laravel & Wordpress specifically. Facebook Github Website