How To Import And Export CSV File In Laravel

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

In this export excel or csv from database tutorial, I will simple create import data to csv, xls file and also we can import data to the database using csv files in laravel 7. So if you do not how how to export and import file in laravel then this example is for you.

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

Step 1 : Install Laravel 8 Project

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

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

 

Step 2: Install Maatwebsite Package

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

composer require maatwebsite/excel

 

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

config/app.php

'providers' => [

	....

	Maatwebsite\Excel\ExcelServiceProvider::class,

],

'aliases' => [

	....

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

],

 

Step 3: Add Routes

Paste the below code to

routes/web.php

Route::get('export', 'DemoController@export')->name('export');
Route::get('importExportView', 'DemoController@importExportView');
Route::post('import', 'DemoController@import')->name('import');

 

Step 4: Create Import Class

In maatwebsite 3 versions provide a way to build an import class and we have to use it in the controller. So it would be a great way to create a new Import class. So you have to run the following command and change the 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 versions provide a way to build an export class and we have to use it in the controller. So it would be a great way to create a new Export class. So you have to run the following command and change the 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 a new controller as MyController. this controller will manage all importExportView, export and import requests and return responses, so put the below content in the controller file:

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 the Last step, let's create import.blade.php(resources/views/import.blade.php) for layout and we will write design code here and put the 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, then leave a comment. 

 

#laravel