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.
In this step, we will install laravel 9 using the following command.
composer create-project --prefer-dist laravel/laravel laravel_9_export_csv
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
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
.
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();
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');
});
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);
}
}
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();
}
}
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>
Now, run the below command in the terminal.
php artisan serve
You might also like: