Home / Snippets / How To Export CSV Data From Laravel
How To Export CSV Data From Laravel cover

How To Export CSV Data From Laravel

2K

2 years ago

0 comments

In this short snippet, you will learn how to export your eloquent models into CSV in Laravel. The benefit of exporting your data into CSV is that it can be considered as a backup and even allow you to create reports which later can be further worked on in Microsoft Excel and the likes.

Method 1: Export CSV Data Using fputcsv Function in Laravel


The first method to export CSV in Laravel is to use the "fputcsv()" function natively provided by Laravel. If you have an array of data like below, you can loop through each of the data and write each array into the CSV using the function as follows.
<?php

$data = [
  [1, 'John Doe', '[email protected]'],
  [2, 'Jane Doe', '[email protected]'],
  [3, 'Papa Doe', '[email protected]'],
];

$handle = fopen('user.csv', 'w');

collect($data)->each(fn ($row) => fputcsv($handle, $row));

fclose($handle);
The content of the CSV will be like below.
1,John Doe,[email protected]
2,Jane Doe,[email protected]
2,Papa Doe,[email protected]

Method 2: Export CSV Data Using Laravel Excel


The second method involves the use of a third-party package called Laravel Excel and this is a great package that allows you to do work on all things "CSV" files.
  • Import CSV
  • Export CSV
  • Customize and Modify CSV
  • Custom Mapping Data
  • More...
Firstly, to install the package you can run the command below.
composer require maatwebsite/excel
Once you have that installed you can export your eloquent model like below. Firstly create a new export type with the artisan command like below.
php artisan make:export UsersExport --model=User
The command above will generate the class below which will export all of the users when the code is executed.
<?php

namespace App\Exports;

use App\Models\User;
use Maatwebsite\Excel\Concerns\FromCollection;

class UsersExport implements FromCollection
{
    public function collection()
    {
        return User::all();
    }
}
Now to call the method you can define a new controller and call the facade to export the data and download it to your local computer.
<?php

namespace App\Http\Controllers;

use App\Exports\UsersExport;
use Maatwebsite\Excel\Facades\Excel;

class UsersController extends Controller 
{
    public function export() 
    {
        return Excel::download(new UsersExport, 'users.csv');
    }
}
Finally, define a route that can be called from the browser to execute the controller method.
Route::get('users/export/', 'UsersController@export');
Now do visit "users/export" endpoint in your browser and you will see "users.csv" in your download folder.
notion avatar

Ahmad Aziz

Week-end developer currently experimenting with web, mobile, and all things programming.

Topics:

Frontend

Resource

Average

Average

Support Us

If you like our tutorial, support us by being our Patreon or buy us some coffee ☕️

Post Tutorials

Learn trending tutorials for ReactJs, React Native, VueJS, NextJs, Laravel, Tailwind CSS, and Flutter development.
View Tutorials

Code Components

Collection of Tailwind CSS components for everyone to use. Browse all of the components that's right for your web projects
View Components

Welcome to PostSrc V3

PostSrc Dark Logo

You have to login to favorite this