Laravel 10 Import Export CSV And EXCEL File

In this article, we will see how to import and export CSV and Excel files in laravel 10 and laravel 11. Here, we will learn about importing and exporting CSV files and Excel files in laravel 10. We will import data to the database using a CSV file in laravel 10/11. In laravel 10, we will use the maatwebsite/excel plugin.

Using this plugin you can easily import and export CSV and Excel files in laravel 10 and laravel 11 examples. For the laravel 10/11 import CSV and Excel file, we will use the import class, for laravel 10 export CSV and Excel files will use the export class.

maatwebsite/excel provides an easy way to import and export CSV files in laravel 10 and laravel 11.

So, let's see Laravel 10 import export CSV and Excel files, how to export CSV files in laravel 10/11, import Excel files in laravel 10, import-export CSV and Excel files in laravel 11

Step 1: Install Laravel 10/11 For Import Export CSV and Excel File

In this step, we will install laravel 10 using the following command,

composer create-project --prefer-dist laravel/laravel laravel_10_import_export_csv_excel

 

 

Step 2: Setup Database Configuration

In this step, we will configure the database configuration. So, open the .env file and add the details. 

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=import_export_csv_excel
DB_USERNAME=root
DB_PASSWORD=root

 

Step 3: Install maatwebsite/exel Package

Now, we will install the maatwebsite package using the below command.

composer require maatwebsite/excel

The Maatwebsite\Excel\ExcelServiceProvider is auto-discovered and registered by default.

If you want to register it yourself, add the ServiceProvider in config/app.php:

'providers' => [
    /*
     * Package Service Providers...
     */
    Maatwebsite\Excel\ExcelServiceProvider::class,
]

The Excel facade is also auto-discovered.

If you want to add it manually, add the Facade in config/app.php:

'aliases' => [
    ...
    'Excel' => Maatwebsite\Excel\Facades\Excel::class,
]

To publish the config, run the vendor publish command:

php artisan vendor:publish --provider="Maatwebsite\Excel\ExcelServiceProvider" --tag=config

This will create a new config file named config/excel.php.

 

Step 4: Create Dummy Records Using Tinker

After adding aliases and providers, we are adding some dummy records in the database using the below command.

php artisan tinker

factory(App\User::class, 100)->create();

 

 

Step 5: Create a New Route

In this step, we are creating a new route in the web.php file.

routes/web.php

<?php
  
use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\ImportExportController;
  
/*
|--------------------------------------------------------------------------
| 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::controller(ImportExportController::class)->group(function(){
    Route::get('import_export', 'importExport');
    Route::post('import', 'import')->name('import');
    Route::get('export', 'export')->name('export');
});

 

Step 6: Add Controller

Now, we will create the ImportExportController using the following command.

php artisan make:controller ImportExportController

After running this command we will add the following code to the controller.

App/Http/Controllers/ImportExportController

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Exports\ExportUsers;
use App\Imports\ImportUsers;
use Maatwebsite\Excel\Facades\Excel;

class ImportExportController 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

After running this command you will find the ImportUsers.php file.

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

After running this command you will find the ExportUsers.php file.

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, we will create the import.blade.php file.

resources/views/import.blade.php

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Laravel 10/11 Import Export CSV And EXCEL File - Techsolutionstuff</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 10/11 Import Export CSV And EXCEL File - Techsolutionstuff</h3>
	<form action="{{ route('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="{{ route('export') }}">Export File</a>
		</div> 
		<button class="btn btn-success">Import File</button>
	</form>
</div>
</body>
</html>

 

Step 7: Run Laravel 10/11 Import Export CSV and Excel File

Now, run the below command in the terminal.

php artisan serve

Open the below URL in the browser.

http://localhost:8000/import_export

 


You might also like:

techsolutionstuff

Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of techsolutionstuff.com. Hailing from India, I craft articles, tutorials, tricks, and tips to aid developers. Explore Laravel, PHP, MySQL, jQuery, Bootstrap, Node.js, Vue.js, and AngularJS in our tech stack.

RECOMMENDED POSTS

FEATURE POSTS