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 8 cURL HTTP Request Example
Laravel 8 cURL HTTP Request Ex...

Hello Friends, Today we will see how to make cURL HTTPs request in your laravel 8 application. This tu...

Read More

Apr-28-2021

Dropdown Filter On Specific Column In Datatable
Dropdown Filter On Specific Co...

In this article, we will see how to add multiple filter dropdowns in datatable. This example is almost identical to...

Read More

Jun-06-2022

Vue Js Get Array Of Length Or Object Length
Vue Js Get Array Of Length Or...

In this tutorial, we will see you example of vue js get an array of length or object length. we will learn about vu...

Read More

Jan-07-2022

CRUD With Image Upload In Laravel 10 Example
CRUD With Image Upload In Lara...

In this article, we will see crud with image upload in laravel 10 examples. Here, we will learn how to image upload with...

Read More

Mar-27-2023