Overriding the “email” attribute on Notifiable Trait Laravel

痞子三分冷 提交于 2020-12-09 06:36:39

问题


I'm trying to use the Notifiable Trait of Laravel in a Model wich doesn't have an email attribute (It has a payer_email in fact)

so I went deep inside in Notifiable trait code and found it uses an routeNotificationFor method from RoutesNotifications Trait so I decided to override it for my desired behavior.

The original method code is:

    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;
    }
}

and I overrided it in my Payment Model in this way:

<?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;
       }
    }
}

But when I test it, does not work. (I've used Notifiable trait on others two models and it works, without the override...)


回答1:


I just created a method named routeNotificationForMail like @Jonathon suggested and I worked like deserved.

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

Thanks him to open my eyes, I was drowning in a glass of water...



来源:https://stackoverflow.com/questions/52429478/overriding-the-email-attribute-on-notifiable-trait-laravel

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!