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.
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\Import_Export_Controller@importExport');
Route::post('import', 'App\Http\Controllers\Import_Export_Controller@import');
Route::get('export', 'App\Http\Controllers\Import_Export_Controller@export');
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:
Hello everyone! I'm excited to share with you how I'm enhancing my Laravel 10 API by enabling the capability to...
Dec-01-2023
In this small artical we will see how to force redirect http to https in laravel, Here i will show you two method in&nbs...
Aug-11-2021
In this small post, we will solve the laravel 8 form class not found error, many time we have received errors like the l...
Mar-12-2021
In this tutorial, I will guide you through the process of adding a datepicker component to your Angular 15 application u...
Jun-28-2023