John Davidson

php - Laravel 8 - Image not deleted from storage + not updating when "Edit"

0 comments
Message:


possible "duplicate": Laravel - Delete images from storage / update post


I want laravel to delete the old image and replace it with new uploaded image in the edit form. I tried doing this:


if( $request->hasfile('violationStatement') ) {
$destination = 'violations/' . $violation->violationStatement;
// Deletes the file if it exists
if( File::exists($destination) ) {
File::delete($destination);
}
$file = $request->file('violationStatement');
$extension = $file->getClientOriginalExtension();
$filename = time() . '.' . $extension;
$file->move('violations/', $filename);
$violation->violationStatement = $filename;
}

$violation->update();

But the image is not deleted nor updating. In addition, I want to have a condition when I just want to edit data without uploading new image...


Here is the routing:


// Traffic Violations
Route::controller(TrafficViolationController::class)->group(function () {
Route::get('violations', 'index')->name('violations.index'); // Index page (DataTable)
Route::get('violations/create', 'create')->name('violations.create'); // The form for adding new records
Route::post('violations/create', 'store')->name('violations.store'); // Add new to DB
Route::get('violations/edit/{violation}', 'edit')->name('violations.edit'); // The form for editing records
Route::put('violations/edit/{violation}', 'update')->name('violations.update'); // Update record to DB
Route::get('violations/{violation}', 'destroy')->name('violations.destroy'); // Delete from DB
});


My Answer:
It looks like you are trying to delete and update images in Laravel 8, but the code you provided may not be working as expected. Here are a few suggestions to help you troubleshoot and fix the issue:

1. Check the file path:
Make sure that the file path you are using to delete the old image and move the new image is correct. You can use the `public_path()` function to get the full path to the public directory.

2. Check file permissions:
Ensure that the storage directory and files have the correct permissions set so that Laravel can delete and move files. You can use the `chmod` command to set the appropriate permissions.

3. Use the Storage facade:
Instead of using the `File` facade, you can use the `Storage` facade provided by Laravel to delete and move files. This can help with managing file operations in a more Laravel-friendly way.

4. Check the form submission:
Make sure that the form submission is correctly sending the file data to the controller. You can use the `dd($request->all())` function in your controller to debug and see the form data being submitted.

5. Add a condition for not uploading a new image:
You can add a condition to check if a new image is uploaded or not before deleting and updating the image. This way, you can handle cases where the user only wants to edit the data without uploading a new image.

By following these suggestions and debugging your code, you should be able to successfully delete and update images in Laravel 8. Let me know if you need further assistance.

Rate this post

3 of 5 based on 6973 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us