何时发送 Laravel 通知(防止重复)
我的项目使用本机 Laravel 通知(存储在数据库表中)。我想在创建新文章时向所有用户发送通知,但是文章模型有一个布尔属性“已发布”,因此我只想在创建新文章时发送通知和 该属性已设置为 true。
因此,在创建新文章时使用观察者发送消息是行不通的,因为此时它尚未发布。另一种选择是监听“updated”事件并在“published”属性为 true 时发送通知,但仅限第一次保存,否则您将在每次保存时继续发送通知。
唯一的解决方案似乎是跟踪为每个相关模型发送的通知,但很难使用通知表来执行此操作,因为您必须使用 json 对象来存储对相关模型的引用。那么最佳实践是什么?为通知表创建附加列?为此创建一个单独的表?
My project uses the native Laravel notifications (stored in a db table). I want to send a notification to all users when a new article is created, however the article model has a boolean attribute 'published', so I only want to send the notifications when a new article has been created and this attribute has been set to true.
So using an observer to send the messages when a new article is created is not going to work, because it has nog been published at that point. Another option is listening to the 'updated' event and sending notifications when the 'published' attribute is true, but only the first save, otherwise you keep sending notifications on each save.
The only solution seems to be to keep track of what notifications are sent for each related model, but it's hard to use the notification table to do this, since you have to use a json object to store a reference to the related model. So what's the best practice? Creating additional columns to the notification table? Creating a seperate table for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
针对您的问题,我会在属性即将更新时检查字段
published
是否脏。因为,如果它已经发布,那么该状态不会改变,所以它基本上会忽略所有后续更新。你可以这样做:Specifically to your issue I'd check if the field
published
is dirty when the attribute is about to be updated. Because, if it's already posted, then that state won't change so it will basically ignore all subsequent updates. You can do this like so: