覆盖可通知特质Laravel上的“电子邮件”属性

时间:2018-09-20 16:28:05

标签: php laravel email

我正在尝试在不具有电子邮件属性(实际上具有 payer_email )的模型中使用Laravel的Notifiable Trait。 >

所以我深入研究了 Notifiable 特性代码,发现它使用了 RoutesNotifications 特性中的 routeNotificationFor 方法,因此我决定对其进行覆盖我想要的行为。

原始方法代码为:

    public function routeNotificationFor($driver)
{
    if (method_exists($this, $method = 'routeNotificationFor'.Str::studly($driver))) {
        return $this->{$method}();
    }

    switch ($driver) {
        case 'database':
            return $this->notifications();
        case 'mail':
            return $this->email;
        case 'nexmo':
            return $this->phone_number;
    }
}

,而我在付款模式中以这种方式覆盖了它:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
use Illuminate\Notifications\Notifiable;
use App\Notifications\PaymentNotify;

class Payment extends Model
{

    use Notifiable;

    public function routeNotificationFor($driver)
    {
       if (method_exists($this, $method = 'routeNotificationFor'.Str::studly($driver))) {
           return $this->{$method}();
       }

       switch ($driver) {
            case 'database':
                return $this->notifications();
            case 'mail':
                return $this->payer_email;
            case 'nexmo':
                return $this->phone_number;
       }
    }
}

但是当我测试它时,它不起作用。 (我已经在其他两个模型上使用了Notifiable特质,并且可以正常工作,没有覆盖...)

1 个答案:

答案 0 :(得分:2)

我刚刚创建了一个名为 routeNotificationForMail 的方法,就像@Jonathon建议的那样,我的工作应有的表现。

public function routeNotificationForMail(){
    return $this->payer_email;
}

感谢他睁开眼睛,我被一杯水淹死了...