H
H
havemanyquestions2019-10-23 02:04:24
Laravel
havemanyquestions, 2019-10-23 02:04:24

Laravel. When to use Events and when Notifications?

Hello.
Action A is performed in the controller. A record is inserted into the database. In principle, there is no need to call any events, because this action does not entail any other actions and is performed only in one place. But I need to notify the user on the frontend about this. When using broadcasting from Notifications, there is no way to specify the channel name, so this option does not suit me (I'm not considering writing my own driver yet). I am using Events. I created the Event. The Event Listener has not even been created yet. In Event'e included broadcasting in the required private channel.
Now I need to use Notifications to the database. Write the same data that goes to the frontend via sockets.
A selection of users will be made. These users will receive notifications in the database:

Notification::send($users, new InvoicePaid($invoice));

Plus, notifications will be sent to instant messengers.
Now the question is where to put this logic? Can I call it in the event itself or create an event listener and call it there?
And one more thing - I can’t understand when to use Events, and when Notifications?
If you start with Events, then there is the possibility of broadcasting, but only I should want notifications, then Notifications are included in the work. And there again we have broadcasting as one of the notification channels. Both there and there the same functionality. Moreover, they also have non-overlapping functionality. The same Notifications can be sent to different channels (broadcasting, database, ...). The question is, why not implement this functionality in events? You will say: "Why, use notifications directly in events and that's it." But then why broadcasting was stuck in events, if it was possible to use all the same Notifications?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question