Laravel 7 Import Export Excel & Csv File Example Tutorial
In this tutorial, we will tell you how to import export excel or CSV in the Laravel Framework (Laravel 7 Import Export Excel & CSV file example tutorial).
In this example, we can easily import-export and download the excel & CSV file from the database using the maatwebsite/excel composer package. so you can follow the below step.
Overview
Step 1: Install Laravel
Step 2: Setting Database Configuration
Step 3: Install Package
Step 4: Add providers and aliases
Step 5: Create Dummy Records and migrate the table
Step 6: Create Route
Step 7: Create Import Class
Step 8: Create Export Class
Step 9: Create Controller
Step 10: Create Blade View
Step 11: Run The Application
Step 1: Install Laravel
We are going to install laravel 7, so first open the command prompt or terminal and go to go to xampp htdocs folder directory using the command prompt. after then run the below command.
1 | composer create-project --prefer-dist laravel/laravel laravel7_import_export |
Step 2: Setting Database Configuration
After complete installation of laravel. we have to database configuration. now we will open the .env file and change the database name, username, password in the .env file. See below changes in a .env file.
1 2 3 4 5 6 | DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=Enter_Your_Database_Name(laravel7_import_export) DB_USERNAME=Enter_Your_Database_Username(root) DB_PASSWORD=Enter_Your_Database_Password(root) |
Step 3: Install Package
Now, we are going to install the Maatwebsite package using the below command.
1 | composer require maatwebsite/excel |
Step 4: Add providers and aliases
We will add below providers and aliases in the “config/app.php” file
1 2 3 4 5 6 7 8 9 10 11 | 'providers' => [ ....... Maatwebsite\Excel\ExcelServiceProvider::class, ], 'aliases' => [ ....... 'Excel' => Maatwebsite\Excel\Facades\Excel::class, ], |
Step 5: Create Dummy Records and migrate the table
Now we will first migrate the table. after then adding a dummy record in the ‘users’ table using the laravel tinker command.
1 | php artisan migrate |
1 2 3 | php artisan tinker factory(App\User::class, 100)->create(); |
Step 6: Create Route
Add the following route code in the “routes/web.php” file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <?php /* |-------------------------------------------------------------------------- | 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('import-export', 'TestController@importExport'); Route::post('import', 'TestController@import'); Route::get('export', 'TestController@export'); ?> |
Step 7: Create Import Class
Now, we will create the import class using the below command.
1 | php artisan make:import ImportUsers --model=User |
ImportUsers.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <?php namespace App\Imports; use App\User; use Maatwebsite\Excel\Concerns\ToModel; class ImportUsers implements ToModel { /** * @param array $row * * @return \Illuminate\Database\Eloquent\Model|null */ public function model(array $row) { return new User([ 'name' => $row[0], 'email' => $row[1], ]); } } ?> |
Step 8: Create Export Class
Now, we will create the Export class using the below command.
1 | php artisan make:export ExportUsers --model=User |
ExportUsers.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?php namespace App\Exports; use App\User; use Maatwebsite\Excel\Concerns\FromCollection; class ExportUsers implements FromCollection { /** * @return \Illuminate\Support\Collection */ public function collection() { return User::get(); } } ?> |
Step 9: Create Controller
Now, We will create the DataTableController using the below command and paste below code in this controller.
1 | php artisan make:controller DataTableController |
DataTableController.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 31 32 33 34 35 36 37 38 | <?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Exports\ExportUsers; use App\Imports\ImportUsers; use Maatwebsite\Excel\Facades\Excel; class TestController extends Controller { /** * @return \Illuminate\Support\Collection */ public function importExport() { return view('import'); } /** * @return \Illuminate\Support\Collection */ public function export() { return Excel::download(new ExportUsers, 'users.xlsx'); } /** * @return \Illuminate\Support\Collection */ public function import() { Excel::import(new ImportUsers, request()->file('file')); return back(); } } ?> |
Step 10: Create Blade View
Finally, We will create import.blade.php file in the “resources/views/” folder directory and paste below code.
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 lang="en"> <head> <title>Laravel 7 Import Export Excel & Csv file example tutorial - XpertPhp</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <form action="{{ url('import') }}" method="POST" name="importform" enctype="multipart/form-data"> {{ csrf_token() }} <div class="form-group"> <label for="file">File:</label> <input id="file" type="file" name="file" class="form-control"> </div> <div class="form-group"> <a class="btn btn-info" href="{{ url('export') }}">Export File</a> </div> <button class="btn btn-success">Import File</button> </form> </div> </body> </html> |
Step 11: Run The Application
We can start the server and run this application using the below command.
1 | php artisan serve |
Now we will run our example using the below Url in the browser.
1 | http://127.0.0.1:8000/import-export |