How To Read CSV File in Laravel 8 Tutorial

Share this Article
Reading Time: 4 minutes

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.

Installation of Laravel Application

Laravel Installation can be done in two ways.

  • Laravel Installer
  • By using composer

Laravel Installer

To install Laravel via Laravel installer, we need to install it’s installer first. We need to make use of composer for that.

$ composer global require laravel/installer

This command will install laravel installer at system. This installation is at global scope, so you type command from any directory at terminal. To verify type the given command –

$ laravel

This command will open a command palette of Laravel Installer.

To create ad install laravel project in system,

$ laravel new blog

With the name of blog a laravel project will be created at your specified path.

By using composer

Alternatively, we can also install Laravel by Composer command create-project.

If your system doesn’t has composer Installed, Learn Composer Installation Steps.

Here is the complete command to create a laravel project-

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

After following these steps we can install a Laravel application into system.

To start the development server of Laravel –

$ php artisan serve

This command outputs –

Starting Laravel development server:

Assuming laravel already installed at 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 –

Sanjay Kumar,,Male
Ashish Kumar,,Male
Vijay Rohila,,Male
Dhananjay Negi,,Male
Monu Thakur,,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.


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;


        echo "<pre>";


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

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


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

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

$ php artisan serve


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

Buy Me a Coffee

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.

Learn Web Development Courses @ $4 only.
Web Development Courses @ $4