
Hello Friends, Today I will teach how to Import Export CSV using Laravel 6.
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 will not provide CSV Import Export directly. So, We have to Install the package Composer in Laravel.
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
Create 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
Think Big Get Big! Happy Coding!
Very helpful. Thank you
Welcome!
Nice post! The information you provided is very helpful if someone is planning to develop the Website. I think Digitalopment is a better way to get all the information about laravel development services.