How to create CSV file in Laravel and MySQL with download CSV option
Using Laravel, we can create CSV file and also we can add download csv option after the csv file is generated. In this topic, we will develop a Laravel 10 application to display some data in a html table with download CSV option to download all data in a csv file witout using any Laravel Package.
We will use Laravel pagination to display data in pages and also we will use Laravel factory to create some dummy data in MySQL database.
This is a very simple application and it is very useful as well. All you have to do select the data in the controller and in the view just display them in html table. For CSV create a controller function to write data using fputcsv() in a file with download option.
Let us create a project in laravel 10. Make sure you have composer and PHP installed in your system. I have given the project name as lara_csv.
This will create a folder named "lara_csv" under the folder where you run the above command.
Step 2 - MySQL Database and Migration
Update Laravel .env file for database details. I am using MySQL database named 'lara_demo', so I updated the file as below:
We will create a new table called "applications" for all application data. We will generate some fake data in this table using Laravel factory. Let us create a migration file for this table. Go to your project folder and run below command for migration from VS code terminal.
Above command will create the migration file name <yyyy_mm_dd_xxxxxx>_create_applications_table.php under database/migrations folder and Application model will be created in app/Models folder.
Update Laravel migration file
Let us update the above migration file as below:
Let us now run the migration to create the default Laravel tables as well as our custom "applications" table. Run below artisan command from project folder to create the tables.
After running migration see the structure of 'applications' table.
Update Application model.
Add some fake data using Laravel Faker
We will create some fake data in the applications table. Let us create a factory for Application model as below:
Update Application Factory as below:
Now, update run() method of DatabaseSeeder.php under database/seeders folder to call the factory we created. I have used 100 here to create 100 rows in applications table.
Now, run database seeder to create the rows:
You can see there are 100 rows created in the "applications" table.
Step 3 - Write Laravel Controller code
Let us first take a look at the home page which lists all applications
It displays the list of applications with a download CSV button. Let us now create the controller using make controiller artisan command as below:
Controller will have below methods:
index() method to display all the applications.
generateCsv() method for generating a csv file with the data for all applications.
index() method
This method selects all data from Application model and loads index view.
We have used Laravel pagination to display 10 rows in a page.
generateCsv() method
Here, we are opening a file using PHP fopen() to write the application details. This file has extension as csv. When user clicks on download csv button, this method will be called and application.csv file will be genearted in public folder and downloaded in user's computer. Note that header with Content-Type as text/csv has to be sent with the response.
Routes
Step 4 - Create the views
We have one view for listing the applications along with the layouts.
I have below files in the resources/views folder
header and master are the layouts. index.blade.php is for listing of the applications.
resources/views/layouts/header.blade.php
resources/views/layouts/master.blade.php
resources/views/index.blade.php
I am using some styles and the stylesheet is given below:
public/css/style.css
Step 5 - Test the application
From the project root, start the php development server using artisan serve command:
From browser run localhost:8000. Verify if application is working correctly. Test below cases:
List Applications with pagination
Download CSV file, make sure all rows are added in csv file