Laravel广播:通知与事件 [英] Laravel Broadcasting: Notification vs Event

查看:483
本文介绍了Laravel广播:通知与事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了有关EventsNotifications的laravel文档,看来我们可以触发一个事件,然后从该事件(使用ShouldBroadcast接口)将其广播到我理解的laravel echo中,另一方面,我们可以使用通知viaBroadcast可以做到相同,所以有什么区别?

I read laravel documentations about Events and Notifications, it seems we can fire an event and from that event (using ShouldBroadcast interface) broadcast it to laravel echo which i understand, in the other hand we can use Notifications viaBroadcast to do the same, so what's the difference?

推荐答案

考虑了很多之后,我发现它们是为不同的东西制成的,这是我的理解:

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

通知:

考虑一下Facebook,每次登录时,您都会看到一堆关于您在旅途中发生的事情的通知,如果您在场,也会看到实时通知.

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

与此同时,您收到有关所需通知的电子邮件. 这正是Laravel Notifications所做的. 您可以在雄辩的模型(例如App\User)上使用notify方法来处理类似OrderApproved之类的内容,该方法可以完成您计划执行的操作,例如向该用户发送短信.并且您还可以将该通知的一瞬间保存在数据库中,以便当用户回来时,他或她可以看到您已经批准了他们的订单.

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

事件:

这是发生某事的时间,例如创建新用户时,并且您想要执行其他操作,例如发送验证电子邮件,发送验证短信等.这就是为什么创建事件以便可以处理该事件的不同逻辑的原因使用监听器. 广播时,您可以在活动中使用ShouldBroadcast界面,并可以从该界面与注册新用户的管理面板同步数据.当管理员正在监视用户列表时,这很有用,而无需重新加载页面,您可以让用户Laravel Echo在管理面板上接收该事件并将新注册的用户追加到列表中.

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.

结论:

这实际上取决于您的需求,如果您只想更新界面中的某些内容,则可能是您需要的事件.但是如果您需要执行更多操作,则可以使用通知.

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

这篇关于Laravel广播:通知与事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆