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

[7.x] Adds "when" method to the Mailable class #31828

Merged
merged 2 commits into from
Mar 8, 2020
Merged

[7.x] Adds "when" method to the Mailable class #31828

merged 2 commits into from
Mar 8, 2020

Conversation

elbojoloco
Copy link
Contributor

This could be useful for example when conditionally attaching files, setting different subjects or maybe even using different views.

Here is an example use case:

return $this->from('[email protected]')
            ->when($cv, function ($message) use ($cv) {
                $message->attach($cv->getRealPath(), [
                    'as' => $cv->getClientOriginalName(),
                    'mime' => $cv->getClientMimeType(),
                ]);
            })
            ->subject('Example subject')
            ->view('mail.example');

Method signature is the exact same as the when method on the Illuminate\Database\Concerns\BuildsQueries trait, maybe for the future it's an idea to extract it (and similar methods) to a more generally reusable trait? One with a slightly different signature can also be found in Illuminate\Support\Collection, I believe they could be used interchangeably.

This could be useful for example when conditionally attaching files, setting different subjects or maybe even using different views.
@GrahamCampbell GrahamCampbell changed the title Adds "when" method to the Mailable class [7.x] Adds "when" method to the Mailable class Mar 7, 2020
*
* @param mixed $value
* @param callable $callback
* @param callable|null $default
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn' $default be mixed?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

something like callable|null or string|int helps you know more what types does this method accept. mixed doesn't make it clear

@taylorotwell taylorotwell merged commit 449a85c into laravel:7.x Mar 8, 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.

4 participants