Don't Forget to Delete Files: Model Events

preview_player
Показать описание
I will show an example of a Model event to delete the related files from storage.

- - - - -
Support the channel by checking out my products:

- - - - -
Other places to follow:
Рекомендации по теме
Комментарии
Автор

To be really sure the cleanup will always work, you'd have to wrap the call to `$document->delete()` in a transaction, so the deletion would be rolled back if deleting the file fails. In most cases this will be fine though. You could have a maintenance job cleaning up unlinked files if it becomes a problem.

mallesbixie
Автор

If you are using soft deletes use self::forceDeleted(function ..., this will only delete the file from storage when you force delete the model.

tqxhoef
Автор

Thanks. Also, this should be added as new method for FileUpload field, maybe like removeFileOnDeleted(true)

haiffy
Автор

Just the thing I needed, was looking for a solution for this few days ago, can't thank you enough...

SD-pffw
Автор

I love it! But often I use Event class. In this way I can call event wherever I want

francescoleto
Автор

How clean your interface and you code is, is mind blowing to me

quebono
Автор

Thank you Povilas. This is something that i needed

fulufheloful
Автор

What about the case when user chooses another file ? The old uploaded file will stay on the file system...I had this problem with fillament v2...

panicmilovan
Автор

how can you wrap that code in an if statement.? My model has a option image upload

kifaru.
Автор

I have created foreign key on post table for user_id column but when i delete user than user posts images are not deleting.

I have created both method deleting and deleted inside boot method of post modal.

amitsolanki
Автор

What if you are using a filament plugin like Spatie Settings to store maybe site logo favicons e.t.c how will you delete the file before uploading anotherone

albanuskelvin
Автор

What about soft deleted would you then delete it or not or wil just ask for a remeber this be deleted forever message or what

kingstalker
Автор

that good. but if multiple file upload how can deal with it ? or just delete image without delete the record

nazarali
Автор

What library are you using for file upload? Seems fancy

cardboarddignity
Автор

Hello, I don't find any related video to ask this question in comment so I ask it here:

Between Markdown and Html which strategy do you prefer for storing blog content in database? How about laravelDaily?

amir_hjr
Автор

What happens if the user uploads a file and then closes the tab/never saves the model? 😅

gilneyn.mathias
Автор

I don’t think it’s the responsibility of a model to delete files, that should be the business/service class. models/entities in Laravel have already too much responsibility

RealHomeboy