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

[8.x] Add when() and unless() methods to MailMessage #34814

Merged
merged 1 commit into from
Oct 13, 2020
Merged

[8.x] Add when() and unless() methods to MailMessage #34814

merged 1 commit into from
Oct 13, 2020

Conversation

KKSzymanowski
Copy link
Contributor

This PR adds when() and unless() methods to the MailMessage class. The implementation is identical to the Illuminate\Database\Concerns\BuildsQueries::when() and Illuminate\Database\Concerns\BuildsQueries::unless() equivalents.

An example use case:

public function toMail(User $user)
{
    return (new MailMessage)
        ->when(
            $this->offer->acceptation_comment,
            function(MailMessage $message, $comment) {
                $message->line('Your offer has been rejected with a comment:')
                        ->line($comment);
            },
            function(MailMessage $message) {
                $message->line('Your offer has been rejected.');
            }
        );
}

During implementation I noticed I could just copy the methods from BuildsQueries trait without any changes, similar with the tests. I think this may be a good time to refactor these methods out of the BuildsQueries trait to something more generic and utilize them in other places without code duplication. I cannot think of any good place for them, though. What do you think?

Removing them from BuildsQueries and putting them somewhere else could however break backwards compatibility if someone uses this trait directly but that would need further testing to confirm.

@GrahamCampbell GrahamCampbell changed the title Add when() and unless() methods to MailMessage [8.x] Add when() and unless() methods to MailMessage Oct 13, 2020
@taylorotwell taylorotwell merged commit 7e56f6e into laravel:8.x Oct 13, 2020
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.

2 participants