Laravel Broadcasting: Notification vs Event

前端 未结 2 855
忘了有多久
忘了有多久 2021-01-30 10:48

I read laravel documentations about Events and Notifications, it seems we can fire an event and from that event (using ShouldBroadcast int

2条回答
  •  走了就别回头了
    2021-01-30 11:23

    After thinking a lot, i found out that they are made for different things, here's what i understood:

    Notifications:

    Consider facebook, everytime you login you see bunch of notifications about things that happened while you where away, also if you are present you see live notifications..

    meanwhile you're getting emails about notifications that you want.. this is exactly what Laravel Notifications is doing. you can use notify method on your eloquent models such as App\User about something like OrderApproved which will do whatever you planned it to do for you like sending sms to that user. and also you can save one instant of that notification on database so when user comes back he or she can see that you have approved their order..

    Events:

    it's when something happens, like when a new user is created and you want to do different things like sending verification email, sending verification sms and.. this is why you create an event so that you could handle different logics of that event using listeners. when it comes to broadcasting, you can use ShouldBroadcast interface on your event and from there you can sync data with your admin panel that a new user is registered. this will be useful when admin is watching list of users and without reloading the page you could user Laravel Echo to receive that event on admin panel and append new registered user to the list.

    Conclusion:

    it really depends on what you need, if you just want to update something in your interface, maybe events are what you need. but if you need to do more you can use notifications.

    in the end events are used when you need to do things when something happens while notifications are report of what just happened.

    hope it help others..

提交回复
热议问题