Laravel 9 Read CSV File Example
Teams applying “Laravel 9 Read CSV File Example” in a production project can use this team workflow page to record implementation, review and debugging time, then compare that effort with tests and shipped functionality instead of treating activity as performance by itself.
Before copying the example into a live application, confirm version-specific behaviour with the official Laravel website and test it against the project’s actual database and runtime.
Hello Friends,
This article will give you example of how to read csv file in laravel9?. Article contains classified information. It will give the complete idea of CSV file reading in laravel 9.
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 9 about CSV file reading.
Let’s get started 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 following step.
composer create-project laravel/laravel example-app 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],FemaleYou 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 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
{
/**
* 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 users.csv file. storage_path() is a Laravel 9 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: Add Route
Open web.php from 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 serveNow, you have to open 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]=>[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...
# Laravel 9
- How to Notifications With database Driver in Laravel 11?
- How to Send Email Via Notification in Laravel 11?
- How to Install and Configure Laravel 11 Debugbar?
- How to Like Dislike System in Laravel 11?
- How to Paginate a Model with Relationship in Laravel 11?
- Laravel 11 Upload Files to Amazon S3 Example
- Laravel 11 Send WhatsApp Messages Example
- Laravel 11 Rest API Authentication Using JWT Tutorial