Don't Forget to Delete Files: Model Events

แชร์
ฝัง
  • เผยแพร่เมื่อ 3 ก.พ. 2025

ความคิดเห็น • 32

  • @sonOfLiberty100
    @sonOfLiberty100 ปีที่แล้ว

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

    • @TheKennyWorld
      @TheKennyWorld ปีที่แล้ว +2

      If you are talking about it the UI, I'm pretty sure that is Filament

    • @sonOfLiberty100
      @sonOfLiberty100 ปีที่แล้ว

      @@TheKennyWorld Thank you

  • @mallesbixie
    @mallesbixie ปีที่แล้ว +4

    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.

  • @SD-pf4fw
    @SD-pf4fw ปีที่แล้ว

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

  • @fulufheloful
    @fulufheloful ปีที่แล้ว

    Thank you Povilas. This is something that i needed

  • @francescoleto2823
    @francescoleto2823 ปีที่แล้ว +1

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

  • @MichaelPritchard-q4q
    @MichaelPritchard-q4q ปีที่แล้ว +1

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

  • @fanievdm1
    @fanievdm1 ปีที่แล้ว

    Good reminder. Thanks.

  • @haiffy
    @haiffy ปีที่แล้ว +2

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

  • @panicmilovan
    @panicmilovan ปีที่แล้ว

    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...

  • @amitsolanki9363
    @amitsolanki9363 11 หลายเดือนก่อน

    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.

  • @kifaru.
    @kifaru. 7 หลายเดือนก่อน

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

  • @alialaa650
    @alialaa650 ปีที่แล้ว

    Happy to learn it thanks

  • @zubayerahamed7771
    @zubayerahamed7771 ปีที่แล้ว

    Happy to learn it

  • @albanuskelvin449
    @albanuskelvin449 ปีที่แล้ว

    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

    • @LaravelDaily
      @LaravelDaily  ปีที่แล้ว

      Not sure I understand the question. Filament Edit form allows you to replace old file with the new file.

  • @cardboarddignity
    @cardboarddignity ปีที่แล้ว

    What library are you using for file upload? Seems fancy

  • @nazar87ali
    @nazar87ali ปีที่แล้ว

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

    • @LaravelDaily
      @LaravelDaily  ปีที่แล้ว

      You just delete files in a loop. Or maybe I misunderstood the question, sorry.

  • @kingstalker
    @kingstalker ปีที่แล้ว

    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

    • @LaravelDaily
      @LaravelDaily  ปีที่แล้ว

      Personal preference: I think Soft Deletes means that there is a possibility to Restore records in the future, so from that angle - files probably shouldn't be deleted either.

    • @kingstalker
      @kingstalker ปีที่แล้ว

      @@LaravelDaily yes you said that before in your videos but im asking what about the file let say your model is user uploads or someting but in migration it say cascade on delete it would not delete the file so how to deal with that

  • @nobel1378
    @nobel1378 ปีที่แล้ว

    Thank you very much

  • @gilney.mathias
    @gilney.mathias ปีที่แล้ว

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

    • @LaravelDaily
      @LaravelDaily  ปีที่แล้ว

      Then there should be a separate Scheduled Job or Artisan command to cleanup old files without models.

  • @amir_hjr
    @amir_hjr ปีที่แล้ว

    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?

    • @modestasmv
      @modestasmv ปีที่แล้ว

      LaravelDaily internally uses a combination of both. We are writing everything in Markdown and then transform that into HTML. Storing it as markdown for edits and HTML for display

  • @GergelyCsermely
    @GergelyCsermely ปีที่แล้ว

    Thanks

  • @dorm762
    @dorm762 ปีที่แล้ว

    nice!

  • @RealHomeboy
    @RealHomeboy ปีที่แล้ว +1

    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