How To Export Data Into CSV File In Laravel 9

In this article, we will see how to export data into a CSV file in laravel 9. Here, we will learn to export CSV files in laravel 9 using maatwebsite/excel package. we will use the maatwebsite/excel package for exporting data. Using this package you can import and export CSV and excel files.

Laravel Excel easily exports collections to CSV files. Also, it provides queue exports for better performance and export bind in blade views.

So, let's see the export CSV files in laravel 9, laravel export CSV, maatwebsite/excel laravel 9, and how to generate CSV in laravel 9.

Step 1: Install Laravel 9 for Export CSV File

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

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

 

Step 2: Setup Database Configuration

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

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

 

 

Step 3: Install maatwebsite/exel Package

Now, we will install the maatwebsite/excel package using the composer command.

composer require maatwebsite/excel

If composer require fails on laravel 9 because of the simple-cache dependency, you will have to specify the psr/simple-cache version as ^2.0 in your composer.json to satisfy the PhpSpreadsheet dependency. You can install both at the same time:

composer require psr/simple-cache:^2.0 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 are adding some dummy records in the database using the below command.

php artisan tinker

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

 

 

Step 5: Create 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\ExportController;
  
/*
|--------------------------------------------------------------------------
| 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(ExportController::class)->group(function(){
    Route::get('index', 'index');    
    Route::get('export/csv', 'exportCSVFile')->name('export.csv');
});

 

Step 6: Add Controller

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

php artisan make:controller ExportController

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

App/Http/Controllers/ExportController

<?php

namespace App\Http\Controllers;

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

class ExportController extends Controller
{
    public function index()
    {
       return view('index');
    }

    public function exportCSVFile() 
    {
        return (new ExportUsers)->download('users.csv', \Maatwebsite\Excel\Excel::CSV);
    }    
}

 

Step 7: 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\Exports\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 8: Create Blade File for View

Now, we will create the index.blade.php file. So, add the following code to that file.

resources/views/index.blade.php

<!DOCTYPE html>
<html lang="en">
<head>
  <title>How To Export Data Into CSV File In Laravel 9 - 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>How To Export Data Into CSV File In Laravel 9 - Techsolutionstuff</h3>
		<div class="form-group">
			<a class="btn btn-info" href="{{ route('export.csv') }}">Export File</a>
		</div>
</div>
</body>
</html>

 

Step 9: Run Export CSV  File Application

Now, run the below command in the terminal.

php artisan serve

 


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