How to Read CSV File in Laravel 9?

Apr 15, 2022 . Admin

Hello Friends,

This article will give you example of how to read csv file in laravel9?. Article contains classified information. It will give the complete idea of CSV file reading in laravel 9.

This tutorial will be super easy to understand and it’s steps are easier to implement in your code as well. If you learn reading CSV file here, you can use the same concept in data seeding to database via CSV file. This is step by step tutorial in laravel 9 about CSV file reading.

Let’s get started following example.

Step 1: Download Laravel

Let us begin the tutorial by installing a new laravel application. if you have already created the project, then skip following step.

composer create-project laravel/laravel example-app
Step 2: CSV Data Preparation

Let’s consider a .csv file in application. We have a users.csv inside /storage folder.

If we open that file, it is looking like this –

Name,Email,Gender
Piyush Kumar,piyush@gmail.com,Male
Mehul Kumar,mehul@gmail.com,Male
Vishal Kumar,vishal@gmail.com,Male
Nikhil Kumar,nikhil@gmail.com,Male
Bhavesh Patel,bhavesh@gmail.com,Female

You can place this .csv file either in /storage folder or /public folder. We will read only by specifying the path.

Step 3: Create Controller

Open project into terminal and run this artisan command.

php artisan make:controller CsvController

It will create a file CsvController.php inside /app/Http/Controllers folder.

Assuming /storage folder.

Open CsvController.php and write this complete code into it.

app/Http/Controllers/CsvController
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class CsvController extends Controller
{   
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {
        $users = [];

        if (($open = fopen(storage_path() . "/users.csv", "r")) !== FALSE) {

            while (($data = fgetcsv($open, 1000, ",")) !== FALSE) {
                $users[] = $data;
            }

            fclose($open);
        }

        echo "<pre>";
        print_r($users);
    }
}
Concept :
if (($open = fopen(storage_path() . "/users.csv", "r")) !== FALSE) {

    while (($data = fgetcsv($open, 1000, ",")) !== FALSE) {
        $users[] = $data;
    }

    fclose($open);
}

Here, we are parsing users.csv file. storage_path() is a Laravel 9 helper function which returns the path upto /storage folder.

If you have taken /public folder then you should use public_path() in place of it.

Now, we can insert the .csv file data into database etc.

Step 4: Create Route routes/web.php
<?php

use App\Http\Controllers\CsvController;

/*
|--------------------------------------------------------------------------
| 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::get("data", [CsvController::class, "index"]);
Run Laravel App:

All steps have been done, now you have to type the given command and hit enter to run the laravel app:

php artisan serve

Now, you have to open web browser, type the given URL and view the app output:

http://localhost:8000/data
Output:
Array(
    [0]=>Array
        (
            [0]=>Name
            [1]=>Email
            [2]=>Gender
        )
    [1]=>Array
        (
            [0]=>Piyush Kumar
            [1]=>piyush@gmail.com
            [2]=>Male
        )
    [3]=>Array
        (
            [0]=>Mehul Kumar
            [1]=>mehul@gmail.com
            [2]=>Male
        )
    [4]=>Array
        (
            [0]=>Vishal Kumar
            [1]=>vishal@gmail.com
            [2]=>Male
        )
    [5]=>Array
        (
            [0]=>Nikhil Kumar
            [1]=>nikhil@gmail.com
            [2]=>Male
        )
    [6]=>Array
        (
            [0]=>Bhavesh Patel
            [1]=>bhavesh@gmail.com
            [2]=>Male
        )
    )

I hope it will help you...

#Laravel 9