In this tutorial you will learn about the Laravel 8 Import Export Excel and CSV File Tutorial and its application with practical example.
In this Laravel 8 Import Export Excel & CSV File tutorial, I’ll show you how to import and export excel/csv file into and from database in laravel 8 using maatwebsite version 3 package with example. I’ll guide you through step by step with example of importing and exporting a csv or excel file using maatwebsite/excel version 3 composer package.
Laravel 8 Import Export Excel and CSV File Tutorial
When we are developing laravel applications, there may situations where we require to import or export large amount of data into and from the application database. Importing and exporting data through excel or csv files seems a good solution here.
In this step by step Laravel 8 Import Export Excel & CSV File example I’ll demonstrate the import and export of csv file in laravel 8. Please follow the steps given below:
Step 1 : Install Laravel 8
First of all we need to create a fresh laravel project, download and install Laravel 8 using the below command
1 |
composer create-project --prefer-dist laravel/laravel blog |
Step 2: Install maatwebsite/excel Package
In this step we will install maatwebsite/excel Package. Use the following compose command to install maatwebsite/excel Package:
1 |
composer require maatwebsite/excel |
Configure Maatwebsite Package
After Installing Maatwebsite package, we need to add service provider and alias in config/app.php file as following.
config/app.php
1 2 3 4 5 6 7 8 9 10 11 |
'providers' => [ ....... Maatwebsite\Excel\ExcelServiceProvider::class, ], 'aliases' => [ ....... 'Excel' => Maatwebsite\Excel\Facades\Excel::class, ], |
Now, use following command to publish Maatwesite Package configuration file:
1 |
php artisan vendor:publish --provider="Maatwebsite\Excel\ExcelServiceProvider" |
Step 3: Create Dummy Records
Now, run the migration to create database table using following artisan command:
1 |
php artisan migrate |
Run the command to generate dummy users:
1 2 |
php artisan tinker User::factory()->count(20)->create() |
Step 4: Add Routes
After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.
routes/web.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\MyController; /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::get('importExportView', [MyController::class, 'importExportView']); Route::get('export', [MyController::class, 'export'])->name('export'); Route::post('import', [MyController::class, 'import'])->name('import'); |
Step 5: Create Import Class
In this step we will create a import class using following artisan command
1 |
php artisan make:import UsersImport --model=User |
After, the above command executed successfully it will create UsersImport.php file in Imports directory.
app/Imports/UsersImport.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?php namespace App\Imports; use App\Models\User; use Maatwebsite\Excel\Concerns\ToModel; use Maatwebsite\Excel\Concerns\WithHeadingRow; class UsersImport implements ToModel, WithHeadingRow { /** * @param array $row * * @return \Illuminate\Database\Eloquent\Model|null */ public function model(array $row) { return new User([ 'name' => $row['name'], 'email' => $row['email'], 'password' => \Hash::make($row['password']), ]); } } |
Step 6: Create Export Class
Now we will create an export class for User model:
1 |
php artisan make:export UsersExport --model=User |
After, the above command executed successfully it will create UsersExport.php file in Exports directory.
app/Exports/UsersExport.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php namespace App\Exports; use App\Models\User; use Maatwebsite\Excel\Concerns\FromCollection; class UsersExport implements FromCollection { /** * @return \Illuminate\Support\Collection */ public function collection() { return User::all(); } } |
Step 7: Create Controller
Now, lets create a controller named MyController using command given below –
1 |
php artisan make:controller <span class="heads">MyController</span> |
app/Http/Controllers/MyController.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Exports\UsersExport; use App\Imports\UsersImport; use Maatwebsite\Excel\Facades\Excel; class MyController extends Controller { /** * @return \Illuminate\Support\Collection */ public function importExportView() { return view('import'); } /** * @return \Illuminate\Support\Collection */ public function export() { return Excel::download(new UsersExport, 'users.xlsx'); } /** * @return \Illuminate\Support\Collection */ public function import() { Excel::import(new UsersImport,request()->file('file')); return back(); } } |
Step 8: Create Blade File
Now, In this step we will create a blade view file:
resources/views/import.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<!DOCTYPE html> <html> <head> <title>Laravel 8 Import Export Excel to database Example</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="card bg-light mt-3"> <div class="card-header"> Laravel 8 Import Export Excel to database Example </div> <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 User Data</button> <a class="btn btn-warning" href="{{ route('export') }}">Export User Data</a> </form> </div> </div> </div> </body> </html> |
Now we are ready to run our example so lets start the development server using following artisan command –
1 |
php artisan serve |
Now, open the following URL in browser to see the output –
1 |
http://localhost:8000/importExportView |