Laravel 8 Read CSV File Example

Laravel 8

Nicesnippets

20-01-2022


Laravel 8 Read CSV File Example

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,[email protected],Male

Mehul Kumar,[email protected],Male

Vishal Kumar,[email protected],Male

Nikhil Kumar,[email protected],Male

Bhavesh Patel,[email protected],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]=>[email protected]

[2]=>Male

)

[3]=>Array

(

[0]=>Mehul Kumar

[1]=>[email protected]

[2]=>Male

)

[4]=>Array

(

[0]=>Vishal Kumar

[1]=>[email protected]

[2]=>Male

)

[5]=>Array

(

[0]=>Nikhil Kumar

[1]=>[email protected]

[2]=>Male

)

[6]=>Array

(

[0]=>Bhavesh Patel

[1]=>[email protected]

[2]=>Male

)

)

I hope it will help you...