Laravel Notifications

Laravel Notifications

Written by Dev Dojo on Feb 2nd, 2018 Views Report Post

Notifying users of various things that happen in your application is super easy to implement thanks to Laravel Notifications. Laravel notifications allow you to send a message to any user via Email, Slack, SMS, and many other channels.

Creating a Notification

To create a new notification you can run the following artisan command:

php artisan make:notification TestMessage

And this will create a new file located at app\Notifications\TestMessage.php with the following contents:

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;

class TestMessage extends Notification
{
    use Queueable;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['mail'];
    }

    /**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        return (new MailMessage)
                    ->line('The introduction to the notification.')
                    ->action('Notification Action', url('/'))
                    ->line('Thank you for using our application!');
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            //
        ];
    }
}

Notice the via method in the code above. This is where you can specify how you want the user to be notified. In the example above we are specifying that we want the user to be notified through email:

return ['mail'];

Since we want them to be notified via mail, our notification will fire the toMail() method when you send a notification to a user.

If you wanted to include Slack notifications you would add 'slack' to the via() method:

return ['mail', 'slack'];

And then your notification would fire a toSlack() method:

public function toSlack($notifiable)
{
    return (new SlackMessage)
                ->content('One of your invoices has been paid!');
}

There are many other notification channels that you can find out about at the official Laravel documentation: https://laravel.com/docs/5.5/notifications

Sending a Notification

Using the Notifiable trait in your User class will allow you to easily send notifications to users:

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;
}

After adding that trait you can now run the following:

$user->notify(new App\Notifications\TestMessage);

And that particular user will be sent the TestMessage notification. Next if you wanted to send it to all your users you can use the Notification facade like so:

Notification::send($users, new App\Notifications\TestMessage);

And that's the basics on sending notifications in your Laravel application. Make sure to check out the official Laravel Documentation to learn how to use notifications in more depth.

Now, you can easily start sending messages to your users using Laravel Notifications.

Comments (0)