Overriding the "email" attribute on Notifiable Trait Laravel

Viewed 1555

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 Answers

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...

Related