Laravel DB Transactions and Observers: Be Careful

Поділитися
Вставка
  • Опубліковано 28 вер 2024

КОМЕНТАРІ • 32

  • @mateussantana4418
    @mateussantana4418 3 місяці тому +1

    That's why I love Laravel framework. Very simple and efficient 😄

  • @julienSibille
    @julienSibille 3 місяці тому

    I'm a big fan of Observers, so thank you very much for the tip !

  • @ramirocortes1054
    @ramirocortes1054 2 місяці тому

    Using Laravel 10, I don't have the 'ShouldHandleEventsAfterCommit' class under "Illuminate\Contracts\Events" directory, so IDE is throwing an 'Undefined type' error. Any clues? Has anyone run into this?

  • @yasserelgammal3696
    @yasserelgammal3696 3 місяці тому

    Great Tip, Thank you 👏

  • @raj-kal
    @raj-kal 3 місяці тому

    thanks for the nice information

  • @chibuikeumezinwa7827
    @chibuikeumezinwa7827 3 місяці тому +1

    Never knew about that handy Interface, cool video. 👍

  • @LoganathanNatarajanlogudotcom
    @LoganathanNatarajanlogudotcom 3 місяці тому

    Great tip!

  • @papababis
    @papababis Місяць тому

    Hello and thank you for your videos. I have a similar case of an observer being triggered inside of a DB transaction. The bug is that the observer triggers twice. Do you think this is related to what you are talking about? Thank you for your time

    • @LaravelDaily
      @LaravelDaily  Місяць тому

      Can't answer without debugging the code, sorry

  • @kirayamato6128
    @kirayamato6128 3 місяці тому

    so what's the difference about that and ShouldDispatchAfterCommit? I dont understand.

    • @LaravelDaily
      @LaravelDaily  3 місяці тому

      That one is for jobs, not observers

  • @drugoviic
    @drugoviic 3 місяці тому +1

    souunds like ShouldHavndleAfterCommit should be implemented by default

  • @thiagorocha2516
    @thiagorocha2516 3 місяці тому +1

    Wow! Didn't know that! Probably this is gonna save me in the future...

  • @sandeepbhambre
    @sandeepbhambre 3 місяці тому

    Can we use ShouldHandleEventsAfterComit in traits such as ActivityLogger trait?

    • @LaravelDaily
      @LaravelDaily  3 місяці тому

      I don't think that would work. But I haven't tried.

  • @chibuzoranyagha3076
    @chibuzoranyagha3076 2 місяці тому

    When used with Filament panel's databaseTransactions(), the Observer events are not fired. Can you confirm this?

    • @LaravelDaily
      @LaravelDaily  2 місяці тому

      Not sure which observer exactly, on which model. Without debugging the actual code, can't answer.

  • @AbdulMoizDev
    @AbdulMoizDev 3 місяці тому

    after implementing this do we need to implement db transactions everywhere to trigger the observer??

    • @LaravelDaily
      @LaravelDaily  3 місяці тому

      No, it's your personal choice whether to use transactions. Search my channel for other videos about transactions.

  • @J87NL
    @J87NL 3 місяці тому +1

    Just curious: I wonder why this isn’t enabled by default, any clue?

    • @valpuia604
      @valpuia604 3 місяці тому

      Because DB transaction is not apply by default

    • @eleftrik
      @eleftrik 3 місяці тому +1

      I guess because, in some scenarios, maybe you actually want your observer to be fired as soon as possible, even if the transaction should fail (e.g. the observer writes something to a log - although I wouldn't use an observer for such a scenario)

    • @ilyasavenok9051
      @ilyasavenok9051 3 місяці тому

      @@eleftrik Where would you write to a log then?

    • @eleftrik
      @eleftrik 3 місяці тому

      @@ilyasavenok9051 It depends. Generally speaking, immediately after data has changed, without using observers or events.

  • @sergeypashkevich574
    @sergeypashkevich574 3 місяці тому +1

    But what about this option "after_commit" in the config file?

    • @LaravelDaily
      @LaravelDaily  3 місяці тому

      That one is for queue jobs, not observers

  • @ゲンスルー-g2q
    @ゲンスルー-g2q 3 місяці тому

    It's crucial to test both successful cases and scenarios where transactions fail to ensure specific jobs are not pushed.

  • @mohammadsalloum1
    @mohammadsalloum1 3 місяці тому

    Very helpful tip, thanks alot.

  • @tomaserlebach9760
    @tomaserlebach9760 3 місяці тому

    Really good! Thanks for this!

  • @7error8lade
    @7error8lade 3 місяці тому

    Just what I needed. Very timely. 😁

  • @sudeshryan8707
    @sudeshryan8707 3 місяці тому

    Thank you very much 💛

  • @alekvga
    @alekvga 3 місяці тому

    Log viewer is a package? I need it. 😮