覆盖 Notifiable Trait Laravel 上的“email”属性

QSo*_*oto 4 php email laravel

我正在尝试在没有email属性的模型中使用 Laravel 的 Notifiable Trait (实际上它有一个payer_email

所以我就在内心深处的法定特征代码,发现它采用了routeNotificationFor从方法RoutesNotifications特质,所以我决定重写它为我所期望的行为。

原方法代码为:

    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;
    }
}
Run Code Online (Sandbox Code Playgroud)

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

<?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;
       }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

QSo*_*oto 5

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

public function routeNotificationForMail(){
    return $this->payer_email;
}
Run Code Online (Sandbox Code Playgroud)

感谢他让我睁开眼睛,我淹死在一杯水里……