Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Added conditional lines to MailMessage #43387

Merged
merged 1 commit into from
Jul 24, 2022

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