Search

Laravel Artisan Generator Command: The make:notification Command

December 7, 2016 —John Koster

The make:notification command is used to generate a new notification class. A name must be supplied to the make:notification command; this value will be the name of the newly generated class.

The following would generate a new notification class named StorageQuotaNotification notification class:

1# Generate a new StorageQuotaNotification class.
2php artisan make:notification StorageQuotaNotification

After the command has executed, a new file will be created at app/Notifications/StorageQuotaNotification.php. The file will contain a new class named StorageQuotaNotification. The following would be the generated class:

1<?php
2 
3namespace App\Notifications;
4 
5use Illuminate\Bus\Queueable;
6use Illuminate\Notifications\Notification;
7use Illuminate\Contracts\Queue\ShouldQueue;
8 
9class StorageQuotaNotification extends Notification
10{
11 use Queueable;
12 
13 /**
14 * Get the notification channels.
15 *
16 * @return array|string
17 */
18 public function via($notifiable)
19 {
20 return ['mail'];
21 }
22 
23 /**
24 * Get the notification message.
25 *
26 * @return \Illuminate\Notifications\MessageBuilder
27 */
28 public function message()
29 {
30 return $this->line('The introduction to the notification.')
31 ->action('Notification Action', 'https://laravel.com')
32 ->line('Thank you for using our application!');
33 }
34}

If the provided class name has already been taken by another notification class, the command will issue an error stating something similar to "Notification already exists!". Additionally, this command is capable of handling nested namespaces and directories; to take advantage of this, simply separate the namespace parts and class name with a directory separator:

1# Generate a new StorageQuotaNotification class in a nested directory
2# with the namespace already set on the generated class.
3php artisan make:notification Users/StorageQuotaNotification

Some absolutely amazing
people

The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.