0
0
Laravelframework~20 mins

Notification channels (mail, database, SMS) in Laravel - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Notification Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when sending a notification with only the mail channel defined?

Consider a Laravel notification class that defines only the toMail method and returns a mail message. What happens when you send this notification to a user?

Laravel
public function via($notifiable) {
    return ['mail'];
}

public function toMail($notifiable) {
    return (new \Illuminate\Notifications\Messages\MailMessage)
        ->line('Hello!')
        ->action('Visit', url('/'))
        ->line('Thank you!');
}
AThe user receives an email with the specified message and action button.
BThe notification is saved only in the database, no email is sent.
CAn error occurs because the SMS channel is not defined.
DNothing happens because the notification class lacks a send method.
Attempts:
2 left
💡 Hint

Think about what the via method controls and what toMail returns.

state_output
intermediate
2:00remaining
What data is stored in the database when using the database notification channel?

Given a Laravel notification that uses the database channel and defines the toDatabase method returning ['invoice_id' => 123, 'amount' => 50], what will be stored in the data column of the notifications table?

Laravel
public function via($notifiable) {
    return ['database'];
}

public function toDatabase($notifiable) {
    return ['invoice_id' => 123, 'amount' => 50];
}
A["invoice_id",123,"amount",50]
BSerialized PHP array of the data
C{"invoice_id":123,"amount":50}
DEmpty string because no toArray method is defined
Attempts:
2 left
💡 Hint

Laravel stores notification data as JSON in the database.

📝 Syntax
advanced
2:00remaining
Which option correctly defines the SMS channel method in a Laravel notification?

In Laravel, to send an SMS notification using Nexmo or Vonage, which method signature and return type is correct inside the notification class?

Apublic function toVonage($notifiable) { return (new \Illuminate\Notifications\Messages\VonageMessage)->content('Your code is 1234'); }
Bpublic function toSms($notifiable) { return 'Your code is 1234'; }
Cpublic function toNexmo($notifiable) { return ['content' => 'Your code is 1234']; }
Dpublic function sms($notifiable) { return new SmsMessage('Your code is 1234'); }
Attempts:
2 left
💡 Hint

Check Laravel's official method name and return type for SMS notifications.

🔧 Debug
advanced
2:00remaining
Why does this Laravel notification fail to send SMS?

Given this notification class, why does the SMS notification not send?

public function via($notifiable) {
    return ['mail', 'database'];
}

public function toVonage($notifiable) {
    return (new \Illuminate\Notifications\Messages\VonageMessage)
        ->content('Hello SMS');
}
AThe content method is deprecated and should be replaced with message.
BThe toVonage method must be named toSms instead.
CThe VonageMessage class is not imported, causing a class not found error.
DThe 'vonage' channel is missing from the via() method array.
Attempts:
2 left
💡 Hint

Check the channels listed in the via method.

🧠 Conceptual
expert
3:00remaining
How does Laravel decide which notification channels to use when sending a notification?

In Laravel notifications, what determines the channels used to deliver a notification, and how can this be customized dynamically per notifiable?

AThe notification channels are determined by the notifiable model's properties automatically without needing the via method.
BThe <code>via</code> method returns an array of channels and can use the $notifiable parameter to customize channels dynamically.
CChannels are set globally in the config/notifications.php file and cannot be changed per notification.
DLaravel always sends notifications to all channels defined in the notification class regardless of the notifiable.
Attempts:
2 left
💡 Hint

Think about how the via method works and its parameters.