Notifications, Events and Listeners
The Laravel framework provides many communication occurrences and observer implementation, allowing for simple notifiable events that occur in your application.
Notifications
In addition to supporting sending emails, Laravel has support for notifications across mulitple channels, including SMS. Notifications can also be stored in a database so that they may be displayed on the application.
Creating Notifications
Notifications are stored within the directory app/Notifications. To create a new notification the make:notification
Aristan command needs to be ran:
-
php artisan make:notification InvoicePaid
Simply add to the array 'database'
to store the notification, see Database Notifications to create the table:
public function via($notifiable)
{
return ['mail', 'database'];
}
Notifications usage example
-
use App\Notifications\NotificationExample;
-
Notification::send($users, new NotificationExample($invoice));
Events and Listeners
Events are a great way to decouple various aspects of your application, events can have multiple listeners that do not depend on each other.
Creating Events and Listeners
Events are stored within the directory app/Events and Listeners within the directory app/Listeners. Events and Listeners are defined in the app/Providers/EventServiceProvider.php directory.
protected $listen = [
'App\Events\EventName' => [
'App\Listeners\ListenerName',
],
];
Then to create the new Events and Listeners run:
-
php artisan event:generate
Events and Listeners usage example
-
use App\Events\EventName;
-
event(new EventName($data));