Laravel read csv file; In this tutorial, you will learn how to read csv files in laravel applications.
How to Read CSV File In Laravel 10
Follow the following steps to read csv file in laravel applications; is as follows:
- Step 1: Create the CSV File
- Step 2: Create the Route
- Step 3: Create the Controller and the readCsv Method
- Step 4: Create the View
- Step 5: Test the Application
Step 1: Create the CSV File
Before you can read data from a CSV file in Laravel, you must first create a CSV file. For this tutorial, we’ll create a CSV file named users.csv
that contains the following data:
id,name,email 1,John Doe,[email protected] 2,Jane Doe,[email protected] 3,Bob Smith,[email protected]
The first line of the CSV file contains the column headers, and each subsequent line contains a row of data.
Step 2: Create the Route
Next, you need to create a route in Laravel that will handle the request to read the CSV file. Open the routes/web.php
file and add the following route:
Route::get('/users', 'UserController@readCsv');
This route maps to a readCsv
method in the UserController
.
Step 3: Create the Controller and the readCsv Method
Create a new controller called UserController
using the following command:
php artisan make:controller UserController
Open the app/Http/Controllers/UserController.php
file and add the following code to the readCsv
method:
public function readCsv() { $filePath = storage_path('app/users.csv'); $file = fopen($filePath, 'r'); $header = fgetcsv($file); $users = []; while ($row = fgetcsv($file)) { $users[] = array_combine($header, $row); } fclose($file); return view('users.index', compact('users')); }
This method opens the users.csv
file using the fopen
function, reads the header row using the fgetcsv
function, reads each subsequent row using a loop, and combines the header and row data into an associative array using the array_combine
function. Finally, it closes the file using the fclose
function and returns the data to a view called users.index
.
Step 4: Create the View
Create a new file called index.blade.php
in the resources/views/users
directory and add the following code:
<table> <thead> <tr> <th>ID</th> <th>Name</th> <th>Email</th> </tr> </thead> <tbody> @foreach ($users as $user) <tr> <td>{{ $user['id'] }}</td> <td>{{ $user['name'] }}</td> <td>{{ $user['email'] }}</td> </tr> @endforeach </tbody> </table>
This view displays the data from the CSV file in an HTML table.
Step 5: Test the Application
To test the application, start the Laravel development server using the following command:
php artisan serve
Then, open a web browser and navigate to http://localhost:8000/users. You should see the data from the users.csv
file displayed in an HTML table.
Conclusion
Reading data from a CSV file in Laravel is a straightforward process that involves opening the file, reading the data, and converting it into an array. With the steps outlined in this tutorial, you can easily read data from a CSV file in your Laravel application.
Recommended Laravel Tutorials
Regenerate response