How To Read CSV File in Laravel 8 Tutorial

Reading Time: 4 minutes
19,399 Views

Inside this article we will see the concept i.e How to read CSV file in laravel 8. 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.

Learn More –

Let’s get started.


Laravel Installation

We will create laravel project using composer. So, please make sure your system should have composer installed. If not, may be this article will help you to Install composer in system.

Here is the command to create a laravel project-

composer create-project --prefer-dist laravel/laravel blog

To start the development server of Laravel –

php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.


CSV Data Preparation

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

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

Name,Email,Gender
Sanjay Kumar,sanjay@gmail.com,Male
Ashish Kumar,ashish@gmail.com,Male
Vijay Rohila,vijay@gmail.com,Male
Dhananjay Negi,dj@gmail.com,Male
Monu Thakur,monu@gmail.com,Female

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


Create Controller

Open project into terminal and run this artisan command.

$ php artisan make:controller SiteController

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

Assuming /storage folder.

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

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class SiteController extends Controller
{
    public function index()
    {
        $students = [];

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

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

            fclose($open);
        }

        echo "<pre>";
        print_r($students);
    }
}

Concept

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

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

    fclose($open);
}

Here, we are parsing students.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.


Create Route

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

//...

use App\Http\Controllers\SiteController;

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

//...

Application Testing

Run this command into project terminal to start development server,

php artisan serve

URL: http://127.0.0.1:8000/data

We hope this article helped you to learn How To Read CSV File in Laravel 8 Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.