Laravel 8 Import Export CSV/EXCEL File Example

Websolutionstuff | Oct-01-2020 | Categories : Laravel PHP

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

 

Step 1: Install Laravel 8 For Import Export

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

 

 

Step 2: Setup Database Configuration

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)

 

Step 3: Install maatwebsite/excel Package

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,
 
],

 

 

Step 4: Create Dummy Records Using Tinker

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();

 

Step 5: Create New Route

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');

 

Step 6: Add Controller

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();
    }
}

 

 

Step 7: Create Import Class

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],
        ]);
    }
}

 

Step 8: Create Export Class

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();
    }
}

 

 

Step 9: Create Blade File for View

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>

 

Step 8: Run Laravel 8 Application

Copy the below command and run it in the terminal.

php artisan serve

After that, add the below URL in the browser.

http://localhost:8000/import_export

 


You might also like:

Recommended Post
Featured Post
Laravel 9 Generate PDF From HTML Using TCPDF
Laravel 9 Generate PDF From HT...

In this article, we will see laravel 9 generate PDF from HTML using TCPDF. Here, we will learn how to integrate tcpdf in...

Read More

Jan-31-2023

Multiple ways to Validate Email Addresses in Laravel 10
Multiple ways to Validate Emai...

Hey there! Have you ever wondered how websites ensure that the email addresses you provide are valid? In this article, I...

Read More

Mar-04-2024

How To Validate Password And Confirm Password In React JS
How To Validate Password And C...

In this article, we will see how to validate password and confirm password in react js. Sometimes we need to add th...

Read More

Sep-14-2022

Next Previous Link Button Pagination in Laravel
Next Previous Link Button Pagi...

Today we will learn next previous link button pagination in laravel, Using paginate method you can easily create paginat...

Read More

Jun-14-2021