Challenge - 5 Problems
Notification Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this Laravel notification code?
Consider this Laravel notification class that sends a mail notification. What will be the subject of the email sent?
Laravel
namespace App\Notifications; use Illuminate\Notifications\Notification; use Illuminate\Notifications\Messages\MailMessage; class WelcomeUser extends Notification { public function via($notifiable) { return ['mail']; } public function toMail($notifiable) { return (new MailMessage) ->subject('Welcome to Our App') ->line('Thank you for joining us!'); } }
Attempts:
2 left
💡 Hint
Look at the subject() method inside the toMail() function.
✗ Incorrect
The subject() method sets the email subject. Here it is set to 'Welcome to Our App'. The line() method adds the email body content.
❓ state_output
intermediate2:00remaining
What is the value of $channels after calling via()?
Given this notification class, what will be the value of $channels when calling via($user)?
Laravel
namespace App\Notifications; use Illuminate\Notifications\Notification; class AlertUser extends Notification { public function via($notifiable) { if ($notifiable->prefers_sms) { return ['nexmo']; } return ['mail', 'database']; } } $user = (object) ['prefers_sms' => false]; $notification = new AlertUser(); $channels = $notification->via($user);
Attempts:
2 left
💡 Hint
Check the condition inside via() and the value of prefers_sms.
✗ Incorrect
Since $user->prefers_sms is false, the else branch returns ['mail', 'database'].
📝 Syntax
advanced2:00remaining
Which option causes a syntax error in this Laravel notification?
Identify which notification class code snippet will cause a syntax error.
Attempts:
2 left
💡 Hint
Check how the new keyword is used with method calls.
✗ Incorrect
Option A misses parentheses after new MailMessage, causing a syntax error because method calls require an object instance.
🔧 Debug
advanced2:00remaining
Why does this notification not send an email?
This notification class is supposed to send an email but does not. What is the cause?
Laravel
namespace App\Notifications; use Illuminate\Notifications\Notification; use Illuminate\Notifications\Messages\MailMessage; class NotifyUser extends Notification { public function via($notifiable) { return ['database']; } public function toMail($notifiable) { return (new MailMessage) ->subject('Alert') ->line('You have a new alert.'); } }
Attempts:
2 left
💡 Hint
Check the channels returned by via() method.
✗ Incorrect
The via() method returns only ['database'], so Laravel does not send mail notifications.
🧠 Conceptual
expert3:00remaining
Which option correctly describes Laravel notification broadcasting?
In Laravel, when using notification broadcasting, which statement is true?
Attempts:
2 left
💡 Hint
Think about the interface and method needed for broadcasting notifications.
✗ Incorrect
To broadcast notifications, the notification class must implement ShouldBroadcast and define toBroadcast method to specify broadcast data.