In my Laravel 7 project, I am renaming images on the upload based on the Station name field. Converting a string to slug using Str::of($name)->slug() see below
Naming the image
public function setStationName($extension, $name)
{
return 'station-' . Str::of($name)->slug() . '.' . $extension;
}
So it will generate a name like station-foo-bar.png and finally storing this name to the photo column to database.
Since the name field is editable so the user will be able to change the name field at any time and if the user is uploading a new image with the name field modified, the new image will have a new name.
Here I am getting trouble.
While updating the record with new image and modified field, if I delete the older image using the getOriginal('photo) method, it is not deleting the image. However, it deletes the image if the name field isn't modified.
Station Model
public function deletePhoto()
{
Storage::disk('public')->delete($this->stationPhotoDir . '/' . $this->getOriginal('photo'));
}
When I dd to check the value of getOriginal('photo') it is returning the correct filename that I want to delete but it is not deleting.
Question:
How to delete old images using the
getOriginal()method?
getOriginalbefore saving in the database ? can you show us where did you calldeletePhoto?getOriginalbut my stupid misunderstanding. Therefore the question doesn't deserve to be here. Thanks a lot.