Read csv file in laravel apps; Through this tutorial, you will learn simple way to read csv file in laravel 10 apps.
Laravel 10 Read CSV File
Using the following steps, you can easily read csv file in laravel apps: as following:
- Step 1: Prepare the CSV file
- Step 2: Create the route and controller method
- Step 3: Create the CSVController and the readCSV method
- Step 4: Create the view
- Step 5: Run the application
Step 1: Prepare the CSV file
First of all, you need to have a CSV file that you want to read. For this example, you will create a simple CSV file called “example.csv” that contains two columns: “name” and “email”. Here’s an example of what the file might look like:
name,email Jolly Fam ,[email protected] Kin Doe,[email protected] Hello Smith,[email protected]
Step 2: Create the route and controller method
Next, you need to create a route in your Laravel application that will handle the CSV file. Open the “routes/web.php” file and add the following route:
Route::get('/csv', 'CSVController@readCSV');
This route maps to a method called “readCSV” in a controller called “CSVController”. Next, you need to create the “CSVController” and the “readCSV” method.
Step 3: Create the CSVController and the readCSV method
Create a new controller called “CSVController” using the following command:
php artisan make:controller CSVController
After that, Open the “app/Http/Controllers/CSVController.php” file and add the following code to the “readCSV” method:
use Illuminate\Http\Request; use Illuminate\Support\Facades\Storage; class CSVController extends Controller { public function readCSV() { $filePath = storage_path('app/example.csv'); $file = fopen($filePath, 'r'); $header = fgetcsv($file); $data = array(); while ($row = fgetcsv($file)) { $data[] = array_combine($header, $row); } fclose($file); return view('csv', compact('data')); } }
This method opens the “example.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 “csv”.
Step 4: Create the view
Create a new file called “csv.blade.php” in the “resources/views” directory and add the following code:
<table> <thead> <tr> <th>Name</th> <th>Email</th> </tr> </thead> <tbody> @foreach ($data as $row) <tr> <td>{{ $row['name'] }}</td> <td>{{ $row['email'] }}</td> </tr> @endforeach </tbody> </table>
This view displays the data from the CSV file in an HTML table.
Step 5: Run 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/csv. You should see the data from the “example.csv” file displayed in an HTML table.
In conclusion, reading data from a CSV file in Laravel is a straightforward process. By following the steps outlined in this article, you should be able to easily read data from a CSV file in your Laravel