kirschbaum-development / nova-mail

A Laravel Nova action that provides a mail sending form for any resource
MIT License
73 stars 25 forks source link

getEmailField() with Eloquent relation? #33

Closed nickbluestone98 closed 3 years ago

nickbluestone98 commented 4 years ago

Rather than needing to make an email field in every single model you want to use the action on is there a way to use an email field from an eloquent relation?

function getEmailField(): string
{
    return $this->user->email;
}
gwleuverink commented 4 years ago

Coincidentally I ran into the same thing today. I used a quick workaround by extending the SendMail action by my own and overwriting the handle method.

I agree that a more granular level of control here might be nice

nickbluestone98 commented 4 years ago

Cheers for the reply, I only needed to use this package in one other Model for the time being so it was quicker to add an email field.

I certainly think this would be a good thing to be made possible in a later release and not too much of an ask.

wendelyleal commented 3 years ago

For those who are in the same situation, my solution was:

public function getEmailAttribute (): string
{
    return $this->user->email;
}

public function getEmailField(): string
{
    return 'email';
}
adammparker commented 3 years ago

I think the solution from @wendelyleal is best and was the original intent. It still allows for as much freedom as you want at the cost of just having to add an additional attribute to your model.

I'm open to suggestions though if anyone has other ways that are just as flexible!

adammparker commented 3 years ago

Closing this due to inactivity. This is still the intended way to access attributes from related models to the mailable model.