In this article, we will see the laravel 8 import and export CSV/EXCEL file example. We will simple create import data to xls, CSV file and also we will import data to the database using CSV file in the laravel 8 application. we can easily import-export and download the CSV and excel file from the database using the maatwebsite/excel composer package. maatwebsite/excel provides an easy way to import and export CSV files in laravel 8 using a database model.
So, let's how to export CSV files in laravel 8, import export excel file in laravel 8, and import CSV file in laravel 8.
Import Export CSV And Excel File In Laravel 8
Step 1: Install Laravel 8 For Import Export
Step 2: Setup Database Configuration
Step 3: Install maatwebsite/excel Package
Step 4: Create Dummy Records Using Tinker
Step 5: Create New Route
Step 6: Add Controller
Step 7: Create Import Class
Step 8: Create Export Class
Step 8: Run Laravel 8 Application
We are creating a new project setup for this example. So, create a new project using the below command.
composer create-project --prefer-dist laravel/laravel import_export
In this step, we will configure a database for the example. So, open the .env file and fill in all details like as below.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database name(import_export_demo)
DB_USERNAME=database username(root)
DB_PASSWORD=database password(NULL)
Now, we will install the maatwebsite package using the below command.
composer require maatwebsite/excel
After that, you need to add providers and alias in your project's config/app.php file
'providers' => [
.......
Maatwebsite\Excel\ExcelServiceProvider::class,
],
'aliases' => [
.......
'Excel' => Maatwebsite\Excel\Facades\Excel::class,
],
After adding aliases and providers. we will add some dummy records in the database using the below command.
php artisan tinker
factory(App\User::class, 200)->create();
In this step, we are creating a new route in the web.php file.
routes/web.php
Route::get('import_export', 'App\Http\Controllers\[email protected]');
Route::post('import', 'App\Http\Controllers\[email protected]');
Route::get('export', 'App\Http\Controllers\[email protected]');
Now, we will create the Import_Export_Controller using the following command.
php artisan make:controller Import_Export_Controller
After running this command we need to add the below code in this controller.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Exports\ExportUsers;
use App\Imports\ImportUsers;
use Maatwebsite\Excel\Facades\Excel;
class Import_Export_Controller extends Controller
{
public function importExport()
{
return view('import');
}
public function export()
{
return Excel::download(new ExportUsers, 'users.xlsx');
}
public function import()
{
Excel::import(new ImportUsers, request()->file('file'));
return back();
}
}
Now, we will create the import class using the below command.
php artisan make:import ImportUsers --model=User
app/Imports/ImportUsers.php
<?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],
]);
}
}
Now, we will create the export class using the below command.
php artisan make:export ExportUsers --model=User
app/Export/ExportUsers.php
<?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::all();
}
}
Now create the import.blade.php file for view.
resources/views/import.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<title>Laravel 8 Import Export CSV/EXCEL File Example - Websolutionstuff</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">
<h3>Laravel 8 Import Export CSV/EXCEL File Example websolutionstuff.com</h3>
<form action="{{ url('import') }}" method="POST" name="importform"
enctype="multipart/form-data">
@csrf
<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>
Copy the below command and run it in the terminal.
php artisan serve
After that, add the below URL in the browser.
You might also like:
- Read Also: How To Delete File From Public Folder In Laravel
- Read Also: How to Create Multi Language Website in Laravel
- Read Also: How To Create Custom Middleware In Laravel
- Read Also: Export Buttons In Datatable