In this tutorial, I am going to cover how to create and export CSV files in Laravel. First, we will create the CSV file using the array or the database and then download that CSV file.
This tutorial is easy to follow and contains step by step instructions on how to create and export CSV files in Laravel.
We will first get the Users from the database, store them in an array, add them to the CSV file and then download the CSV file.
Let’s get started.
Table of Contents
Step 1: Install Laravel
First of all, you need to install the laravel if you haven’t done it yet. You can start the project from scratch or you can either choose the existing one. I am going to start from scratch.
composer create-project --prefer-dist laravel/laravel learn_csv
Step 2: Update the ENV file and run migrations
In this step, you need to update the DB credentials in the ENV
file and run the following command for the migrations.
php artisan migrate
Add 2 or 3 rows in the database in the user
table, so that we can see some data in the CSV file.
Step 3: Create a controller
Now we are going to create a CsvController
.
php artisan make:Controller CsvController
Step 4: Create Route
Now we will create a Route in web.php file.
Route::get('get_csv', [\App\Http\Controllers\CsvController::class, 'get_csv'])->name('get_csv');
Step 5: Adding get_csv function
Create the function get_csv
and Include use Response;
and use File;
in the header.
In get_csv
function, first we are getting all the users from the database
$users = User::get();
Then creating headers for the CSV file. They are just the metadata and headers for the CSV file.
$headers = array(
'Content-Type' => 'text/csv'
);
Since I want to store the CSV file in the public >> files folder. So that why I am creating the “files” folder.
if (!File::exists(public_path()."/files")) {
File::makeDirectory(public_path() . "/files");
}
Now in the code below, we are creating the file download.csv
in public >> files folder.
$filename = public_path("files/download.csv");
$handle = fopen($filename, 'w');
Adding the first row of Name
and Email
as the headers.
fputcsv($handle, [
"Name",
"Email",
]);
Now we will iterate the users
array and put them in the CSV file row by row.
foreach ($users as $each_user) {
fputcsv($handle, [
$each_user->name,
$each_user->email,
]);
}
Now we need to close that file.
fclose($handle);
Now the last step is to download the file. We can you Response
to do that.
return Response::download($filename, "download.csv", $headers);
Here is the complete CsvContoller.php
file.
<?php
namespace App\Http\Controllers;
use App\Models\User;
use Illuminate\Http\Request;
use Response;
use File;
class CsvController extends Controller
{
//
public function get_csv(){
$users = User::get();
// these are the headers for the csv file. Not required but good to have one incase of system didn't recongize it properly
$headers = array(
'Content-Type' => 'text/csv'
);
//I am storing the csv file in public >> files folder. So that why I am creating files folder
if (!File::exists(public_path()."/files")) {
File::makeDirectory(public_path() . "/files");
}
//creating the download file
$filename = public_path("files/download.csv");
$handle = fopen($filename, 'w');
//adding the first row
fputcsv($handle, [
"Name",
"Email",
]);
//adding the data from the array
foreach ($users as $each_user) {
fputcsv($handle, [
$each_user->name,
$each_user->email,
]);
}
fclose($handle);
//download command
return Response::download($filename, "download.csv", $headers);
}
}
Conslusion:
Hope today in the above article you have learned how to create and save CSV file in laravel.
Let me know if you are facing any problems in the comments section.
Keep Learning, see you again 🙂
Write a Reply or Comment