How to Export CSV File in Laravel 10 using League/csv

Hey there! 👋 Ever wondered how to export data to a CSV file in Laravel effortlessly? Well, I've got you covered! In this guide, I'll walk you through a straightforward way to achieve this using the league/csv package.

Picture this: You have a bunch of data in your Laravel application that you want to share or analyze in a spreadsheet. The good news is, with just a few steps, you can create a CSV export feature that turns your data into a neat, downloadable file.

We'll be creating a controller that handles the export process, utilizing the power of the league/csv package to make our lives easier. Trust me, it's simpler than you might think!

In this article, we'll see how to export CSV using league/csv in laravel 8, Laravel 9, and Laravel 10.

Let's dive in and get your CSV export up and running quickly. 🚀

Here's a step-by-step guide on how to export a CSV file using league/csv in Laravel:

Step 1: Install Laravel

If you haven't already, create a new Laravel project:

composer create-project --prefer-dist laravel/laravel laravel-10-league-csv-export
cd laravel-10-league-csv-export

 

Step 2: Install league/csv Package

If you haven't installed the league/csv package yet, you can do so using Composer. Open a terminal and run the following command:

composer require league/csv

 

Step 3: Create a Controller

Create a controller that will handle the CSV export. You can use Artisan to generate a new controller:

php artisan make:controller CsvExportController

Open the generated controller (CsvExportController.php) in your preferred code editor and add the following code:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use League\Csv\Writer;

class CsvExportController extends Controller
{
    public function export()
    {
        // Sample data (replace this with your actual data)
        $data = [
            ['Name', 'Email', 'Phone'],
            ['John Doe', 'john@example.com', '123-456-7890'],
            ['Jane Doe', 'jane@example.com', '987-654-3210'],
        ];

        // Create a CSV writer instance
        $csv = Writer::createFromString('');

        // Insert data into the CSV
        $csv->insertAll($data);

        // Set the CSV response headers
        $headers = [
            'Content-Type' => 'text/csv',
            'Content-Disposition' => 'attachment; filename="export.csv"',
        ];

        // Return the CSV as a response
        return response($csv->output(), 200, $headers);
    }
}

In this example, the export method generates a simple CSV file with sample data. Replace the $data array with your actual data.

 

Step 4: Define a Route

Open the routes/web.php file and add a route to your CSV export controller:

use App\Http\Controllers\CsvExportController;

Route::get('/export-csv', [CsvExportController::class, 'export']);

 

Step 5: Test the Export

Run your Laravel development server:

php artisan serve

 

Conclusion:

In conclusion, exporting data to a CSV file in Laravel using the league/csv package is a straightforward process. By following the steps outlined above, I was able to create a CSV export feature in my Laravel application effortlessly.

 


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