Import Export CSV File in Laravel 7.

Hello Friends, Today I will teach how to Import Export CSV using Laravel 7.

Basically, CSV using, in general, to get the list of Data like Company wants its employee data for months end. So System can easily Generate a list of Data of its employees. It’s Generated data in CSV format so it can be stored in our system.

So, Let’s Start our tutorial.


Output:


Step 1: Install-Package of Excel.

Laravel 7 will not provide CSV Import Export directly. So, We have to Install the package Composer in Laravel 7.

Open Your Terminal and put the below Code.

composer require maatwebsite/excel

After Successfully install the package now move to the next step.


Step 2: Changes in Config File.

Now Open your config/app.php file and put the below code.

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

Step 3: Create a Dummy Record

In this Step, We have to create a Dummy Record. So, Open your terminal and put the below Code:

php artisan tinker

factory(App\User::class, 30)->create();

Step 4: Create an Import- Export Class

So, Open your Terminal and put the below code:

php artisan make:import Csv --model=User

Now open your app/Imports/Csv.php and put the below code:

<?php
namespace App\Imports;

use App\User;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithHeadingRow;

class Csv implements ToModel{
    public function model(array $row){
        return new User([
            'name'      =>  $row["name"],
            'email'     =>  $row["email"],
            'password' => \Hash::make($row['password']),
        ]);
    }
}

Same process for Export Class. Open your terminal and put the below code:

php artisan make:export Csv2 --model=User

After the run Command, You have to open your app/Exports/Csv2.php and put the below code:

<?php
namespace App\Exports;

use App\User;
use Maatwebsite\Excel\Concerns\FromCollection;

class Csv2 implements FromCollection{
    public function collection(){
        return User::all();
    }
}

Step 5: Create a Controller.

Now, We have to create new Controller CsvImportExportController.php

So open your terminal and put the below code:

php artisan make:controller CsvImportExportController

Open the Controller put the below Code:

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Exports\Csv2;
use App\Imports\Csv;
use Maatwebsite\Excel\Facades\Excel;

class CsvImportExportController extends Controller{
	public function CsvImportExportView(){
    	return view('viewcsv');
    }
    public function export(){
    	return Excel::download(new Csv2, 'users.csv');
    }
    public function import(){
    	Excel::import(new Csv,request()->file('file'));
    	return back();
    }
}

You can Export File in .csv and .xls format.


Step 6: Create a route

So open your web.php and put the below code:

Route::get('viewcsv', 'CsvImportExportController@CsvImportExportView');
Route::get('export', 'CsvImportExportController@export')->name('export');
Route::post('import', 'CsvImportExportController@import')->name('import');

Step 7: Create a blade File.

Now Create a new blade file viewcsv.blade.php and put the below code.

<!DOCTYPE html>
<html>
   <body>
      <div class="container">
         <div class="card">
            <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</button>
                  <a class="btn btn-danger" href="{{ route('export') }}">Export</a>
               </form>
            </div>
         </div>
      </div>
   </body>
</html>

Now Open your browser and put the below code:

http://localhost:8000/viewcsv

I hope it will help you!

About Dhaval Shah

My name is Dhaval Shah. I'm a Laravel developer. I live in India and I love to write tutorials and tips that can help other developers. I am a big fan of PHP, Java-script, JQuery, Laravel, WordPress, and Bootstrap.
Follow me on Github / Linkedin

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!