Skip to content

Conversation

shino47
Copy link
Contributor

@shino47 shino47 commented Jul 23, 2022

Currently, If we need to add a line to a MailMessage based on a condition, we have to do something like this:

public function toMail($notifiable)
{
    $message = new MailMessage();
    $message->greeting('Hello there!');
    $message->line('Your order has been canceled');
    if ($this->amount > 0) {
        $message->line("The refunded amount is {$this->amount}");
    }
    $message->line('...');
    return $message;
}

With these methods, we can do:

public function toMail($notifiable)
{
    return (new MailMessage)
        ->greeting('Hello there!')
        ->line('Your order has been canceled')
        ->lineIf($this->amount > 0, "The refunded amount is {$this->amount}")
        ->line('...');
}

Which looks similar to the docs.

@shino47 shino47 marked this pull request as ready for review July 23, 2022 21:01
@taylorotwell taylorotwell merged commit 3a0ad1a into laravel:9.x Jul 24, 2022
@mateusjatenee
Copy link
Contributor

mateusjatenee commented Jul 24, 2022

I wonder if using the Conditionable trait isn't a better approach here — that'd allow us to call any methods based on a condition.

Edit: nevermind, just saw that MailMessage uses Conditionable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants