Laravel 10 Read CSV File Example

27-Apr-2023

.

Admin

Laravel 10 Read CSV File Example

Hello Friends,

This article will give you an example of how to read csv file in Laravel 10. The article contains classified information. It will give the complete idea of CSV file reading in Laravel 10.

This tutorial will be super easy to understand and its steps are easier to implement in your code as well. If you learn to read CSV files here, you can use the same concept in data seeding to the database via CSV files. This is the step-by-step tutorial in Laravel 10 about CSV file reading.

Let’s get started with the 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 the following step.

composer create-project laravel/laravel example-app

Step 2: CSV Data Preparation

Let’s consider a .csv file in the 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: Add Controller

Open the project into the terminal and run this artisan command.

php artisan make:controller CsvController

It will create a file CsvController.php inside the /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);

}

}

Here, we are parsing the users.csv file. storage_path() is a Laravel 10 helper function that returns the path up to /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 the database, etc.

Step 4: Add Route

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

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 the web browser, type the given URL and view the app output:

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 10