Challenge - 5 Problems
Mailable Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this Mailable's build method?
Consider this Laravel Mailable class. What will be the subject of the email when this Mailable is sent?
Laravel
class WelcomeMail extends Mailable { public function build() { return $this->subject('Welcome!')->view('emails.welcome'); } } $mail = new WelcomeMail(); $subject = $mail->build()->subject;
Attempts:
2 left
💡 Hint
Remember how Laravel's Mailable class handles chaining and property access.
✗ Incorrect
The `subject()` method sets the `$subject` property (publicly accessible dynamic property) and returns `$this`. `build()` chains this and returns the instance, so `$mail->build()->subject` returns `'Welcome!'`.
❓ state_output
intermediate2:00remaining
What data is available in the email view?
Given this Mailable class, what variables will be available inside the Blade view 'emails.order'?
Laravel
class OrderShipped extends Mailable { public $order; public function __construct($order) { $this->order = $order; } public function build() { return $this->view('emails.order'); } } $order = ['id' => 123, 'total' => 49.99]; $mail = new OrderShipped($order);
Attempts:
2 left
💡 Hint
Public properties of the Mailable are automatically passed to the view.
✗ Incorrect
Laravel automatically makes all public properties of the Mailable class available as variables in the email view. Here, $order is public, so it is accessible in the Blade template.
📝 Syntax
advanced2:00remaining
Which option correctly queues this Mailable?
You want to send the Mailable asynchronously using Laravel's queue system. Which code snippet correctly queues the Mailable?
Laravel
class NotificationMail extends Mailable { public function build() { return $this->view('emails.notify'); } } // Assume $user is a valid User model instance
Attempts:
2 left
💡 Hint
Check Laravel's Mail facade methods for queuing emails.
✗ Incorrect
The Mail facade has a queue() method to send mail asynchronously. Option A correctly uses Mail::to()->queue() with a new Mailable instance.
🔧 Debug
advanced2:00remaining
Why does this Mailable fail to send with an error about missing view?
This Mailable class throws an error when sending: "View [emails.invoice] not found." What is the most likely cause?
Laravel
class InvoiceMail extends Mailable { public function build() { return $this->view('emails.invoice'); } } Mail::to('user@example.com')->send(new InvoiceMail());
Attempts:
2 left
💡 Hint
Check the file path and name of the Blade template.
✗ Incorrect
Laravel looks for the Blade view file at resources/views/emails/invoice.blade.php. If this file is missing, it throws a 'View not found' error.
🧠 Conceptual
expert2:00remaining
What happens if you call build() twice on the same Mailable instance?
Consider this code snippet. What will be the result of calling build() twice on the same Mailable object?
Laravel
class AlertMail extends Mailable { public function build() { return $this->subject('Alert')->view('emails.alert'); } } $mail = new AlertMail(); $first = $mail->build(); $second = $mail->build(); $subjectFirst = $first->subject; $subjectSecond = $second->subject;
Attempts:
2 left
💡 Hint
Remember what build() returns and how subject() works internally.
✗ Incorrect
`build()` returns `$this` each time and can be called multiple times. `subject()` sets the publicly accessible `$subject` property each call. `$first` and `$second` reference the same instance, so both access `'Alert'`.