How to Read CSV File in Laravel 8?


Share:
 | Published By: Admin | Published On: Jan 20, 2022 | Category: Laravel 8

Hello Friends,

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

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 8 about CSV file reading.

Let’s get started following example.

Step : 1 - Installation of Laravel Application

Use this command then download laravel project setup :

composer create-project --prefer-dist laravel/laravel blog
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
{
    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 8 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

Open web.php from /routes folder. Add this route into it.

//...

use App\Http\Controllers\CsvController;

Route::get("data", [CsvController::class, "index"]);

//...

Open project to terminal and type the command to start development server

php artisan serve
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...

Share: