猿问

Eloquent 模型的 Laravel 通知

我正在尝试使用来自非用户 Eloquent 模型的 Notifiable trait (notify()) 方法发送通知(电子邮件)


驱动模型


namespace App;


use Illuminate\Foundation\Auth\User as Authenticatable;

use Illuminate\Notifications\Notifiable;


class Driver extends Authenticatable

{

  use Notifiable;

}

并发送我正在使用的通知


  $driver->notify(new \App\Notifications\Driver\DriverAlloted($booking));

其中 $driver 是一个雄辩的模型实例(app\Driver.php),\App\Notifications\Driver\DriverAlloted 是通知;


如果我用它工作的任何用户 (App\User) 替换驱动程序,我怎样才能让它为 App\Driver 工作


隔江千里
浏览 100回答 1
1回答

慕勒3428872

当通知请求频道时mail,Laravel 从特征中调用此代码Illuminate\Notifications\RoutesNotifications(由特征扩展Illuminate\Notifications\Notifiable)/**     * 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);        }        switch ($driver) {            case 'database':                return $this->notifications();            case 'mail':                return $this->email;        }    }因此,您有两种解决方法:您的Driver模型需要有一个email属性,该属性将成为邮件到达的路线。在您的模型中定义一个routeNotificationForMail方法,该方法将以自定义方式计算路线/** * @param  \Illuminate\Notifications\Notification|null  $notification * @return string */public function routeNotificationForMail($notification){    return $this->address;}
随时随地看视频慕课网APP
我要回答