master

laravel/framework

Last updated at: 29/12/2023 09:24

RoutesNotifications.php

TLDR

This file contains a trait called "RoutesNotifications" that provides methods for sending notifications and getting routing information for notifications.

Methods

notify

This method sends the given notification by using the Laravel notification dispatcher.

notifyNow

This method sends the given notification immediately by using the Laravel notification dispatcher. It allows specifying the notification channels to use.

routeNotificationFor

This method gets the notification routing information for the given driver. It first checks if there is a specific method for the driver name appended with "routeNotificationFor" in the class. If such a method exists, it calls it with the notification as a parameter. If not, it returns routing information based on the driver type, such as returning the notifications table for the "database" driver or the email attribute for the "mail" driver.

<?php

namespace Illuminate\Notifications;

use Illuminate\Contracts\Notifications\Dispatcher;
use Illuminate\Support\Str;

trait RoutesNotifications
{
    /**
     * Send the given notification.
     *
     * @param  mixed  $instance
     * @return void
     */
    public function notify($instance)
    {
        app(Dispatcher::class)->send($this, $instance);
    }

    /**
     * Send the given notification immediately.
     *
     * @param  mixed  $instance
     * @param  array|null  $channels
     * @return void
     */
    public function notifyNow($instance, array $channels = null)
    {
        app(Dispatcher::class)->sendNow($this, $instance, $channels);
    }

    /**
     * Get the notification routing information for the given driver.
     *
     * @param  string  $driver
     * @param  \Illuminate\Notifications\Notification|null  $notification
     * @return mixed
     */
    public function routeNotificationFor($driver, $notification = null)
    {
        if (method_exists($this, $method = 'routeNotificationFor'.Str::studly($driver))) {
            return $this->{$method}($notification);
        }

        return match ($driver) {
            'database' => $this->notifications(),
            'mail' => $this->email,
            default => null,
        };
    }
}