Laravel在文章发布时发送通知

I have a Laravel app where users can subscribe to a blog tags and they need to receive notifications when an article is added to the tags they subscribed to. But we have specific needs that make it more complex :

  • An article can have many tags (added anytime)
  • An article can have a publishing date in the future
  • An article can have a draft state and so it's not yet visible for the visitors

So if an admin create an article in draft mode, we shouldn't send notification until it remove it from draft mode and the publication date has arrived.

My problem is that because I don't know when the admin will remove the draft mode I can't create queued notification with a delay. Also because he can add a tag anytime I can't just send all the notifications when the draft mode is removed because he can come back later and add an other tag...

The only option that I have in mind is to save in a pivot table any changes that happend before it was published and when it's published send all notifications. But it feels a little weird.

I hope some of you had this kind of multi-state model that need to release some notifications, or do some actions, when it's visible to the users...

I think model events should be a good solution for you :

https://laravel.com/docs/5.8/eloquent#events

It gives you the opportunity to run some logic before (or after) creating/updating/deleting a model. So every time a post/tag/post_tag is updated (or created), you will be able to check if it became published or if it got a new tag.

Let me know if it helped !