Model event for deleting files #5387
DevMushref
started this conversation in
Showcase
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I've found few discussions here requesting/asking how to delete files from storage. So here is how I did it.
In your model file
Create a public function called
delete()
.Within it you have few options to make
1- If the file is single and NOT required:
2 - If the file is single and IS required:
3- If the files are multiple and are NOT required:
4- If the files are multiple and ARE required:
Why
is_null()
?When I tried to delete a record that doesn't have multiple images uploaded, I get an error saying that the image directory is NULL.
So what I basically did is tell the software that if the array is NOT null aka
!is_null()
and the files DO exist, then delete them.Don't forget to add
parent::delete();
at the end of yourdelete()
method to delete the records too.How does the code read the directory URL?
It always saves in the
storage/
folder. In your Filament resource, if you set the directory then it will be part of the$record
saved in database.For example:
I have a
FileUpload
filed that saves files in a folder calledreports
:If you want to call the image in a custom blade then you should call it like so:
<img src="storage/ {{ $record->attachment }}" />
The URL will be
storage/ + reports/image.png
. If no directory was set:storage/ + image.png
.So always keep in mind that Filament saves the directory name (if available) as well as the image name.
Hope it helped in your case!
Thank you for reading.
Beta Was this translation helpful? Give feedback.
All reactions