The provided code implements a CSV import feature in a Laravel application to update property information from a CSV file. It comprises a route, a controller method, and a corresponding view for uploading a CSV file.
1. Route Definition:
Route::get('/update_property', 'UpdatePrpoertyController@src');
Route::post('/import-csv', 'UpdatePrpoertyController@importCSV')->name('importCSV');
The route definition sets up endpoints for displaying the update property page and handling the CSV import request. The importCSV
route is mapped to the importCSV
method in the UpdatePrpoertyController
.
2. Controller Method (importCSV
):
public function importCSV(Request $request) {
// Handles the file upload and updates property information based on the CSV data
// Retrieves CSV file, processes it, and updates the 'day_of_schedule' column in the 'properties' table
// Validation check if file is present
if ($request->hasFile('file')) {
// Retrieve file
$file = $request->file('file');
// Read CSV file
$csvData = array_map('str_getcsv', file($file));
// Loop through CSV rows and update database
foreach ($csvData as $data) {
DB::table('properties')->where('id', $data[0])
->update([
'day_of_schedule' => $data[1],
]);
}
return redirect()->back()->with('success', 'CSV file imported and data updated successfully.');
}
// Redirect back if no file was uploaded
return redirect()->back()->with('error', 'Please upload a CSV file.');
}
This method checks if a file is present in the request. If a file is uploaded, it reads the CSV file, processes its data, and updates the 'day_of_schedule' column in the 'properties' table using the DB
facade.
3. View Code: The view contains a form for file upload and a button to trigger the CSV import.
<div class="row">
<div class="col-md-12">
<div class="card card-primary card-outline">
<form method="post" action="{{ route('importCSV') }}" enctype="multipart/form-data">
@csrf
<div class="row" style="padding:25px">
<div class="col-md-6">
<div class="form-group">
<label for="exampleInputEmail1">Upload CSV </label>
<input type="file" class="form-control rounded-0 extra-filter" id="file" name="file">
</div>
</div>
<div class="col-md-12">
<button type="submit" class="btn btn-primary"> Update Property Day </button>
</div>
</div>
</form>
<div class="table-container">
</div>
</div>
</div>
</div>
Create New middleware for the route. php artisan make:middleware MyrouteMiddleware ..
Create a New Middleware php artisan make:middleware NameOfMiddleware This is..
Middleware can verify the user's authentication status and ensure they have the necessary credential..
We have created a view page name as login.blade.php <div class="col-sm-4"><h3>L..
Suppose you have data and you need to check it by clicking or page onload. first, create a controll..
Blade is a templating engine used in Laravel, a popular PHP framework. It provides a simple and expr..
Current Url <div class="container-fluid p-5 bg-primary text-white text-center"><h1..
A component is a group of code like a header or footer. Which are re-useable. Common part ofapplicat..
In Laravel, a controller is a class that handles the logic and acts as an intermediary between themo..
It's Easy to create routes. Already we know That all route files should be kept in the routes ..
The blade is the templating engine used in Laravel, which is a popular PHP framework. It provides at..
Laravel is a popular open-source PHP framework known for its elegant syntax, expressive features, an..
In Laravel, you can set dynamic page titles by utilizing the power of Blade templating and passing d..
To set up a Laravel project with Bootstrap, you can follow these steps: Create a project, I..
How to Install Laravel using Composer First, we need to install Composer. check How to inst..
How to Install Composer on Linux Open terminal php -r "copy('https://getcomposer.o..
Get the latest news and updates by signing up to our daily newsletter.We won't sell your email or spam you !