Bird
Raised Fist0
Laravelframework~20 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. In Laravel notifications, which method defines the channels a notification will be sent through?
easy
A. via
B. send
C. channels
D. notify

Solution

  1. Step 1: Understand the notification class structure

    Laravel notifications use a method to specify which channels to send the notification through.
  2. Step 2: Identify the method name

    The method that returns an array of channels is named via.
  3. Final Answer:

    via -> Option A
  4. Quick Check:

    Channel selection method = via [OK]
Hint: Remember: 'via' means 'through' which fits channel selection [OK]
Common Mistakes:
  • Confusing 'send' with 'via' method
  • Using 'channels' instead of 'via'
  • Assuming 'notify' defines channels
2. Which of the following is the correct syntax to specify the mail channel in the via method of a Laravel notification?
easy
A. return mail();
B. return 'mail';
C. return ['mail'];
D. return ['MailChannel'];

Solution

  1. Step 1: Check the return type of the via method

    The via method must return an array of channel names as strings.
  2. Step 2: Identify the correct array syntax

    Returning ['mail'] is correct; returning a string or function call is incorrect.
  3. Final Answer:

    return ['mail']; -> Option C
  4. Quick Check:

    Channel list must be an array of strings [OK]
Hint: Always return an array of channel names in via [OK]
Common Mistakes:
  • Returning a string instead of an array
  • Calling a function instead of returning array
  • Using incorrect channel class names
3. Given this Laravel notification code snippet, what will be stored in the database when the notification is sent?
public function toDatabase($notifiable) {
    return [
        'invoice_id' => 1234,
        'amount' => 250
    ];
}
medium
A. Only the 'invoice_id' value 1234
B. An array with keys 'invoice_id' and 'amount' and their values
C. A JSON string of the notification class name
D. An error because toDatabase must return a string

Solution

  1. Step 1: Understand the toDatabase method role

    The toDatabase method returns an array of data saved in the notifications table.
  2. Step 2: Analyze the returned array

    The array contains keys 'invoice_id' and 'amount' with values 1234 and 250 respectively.
  3. Final Answer:

    An array with keys 'invoice_id' and 'amount' and their values -> Option B
  4. Quick Check:

    toDatabase returns array data saved in DB [OK]
Hint: toDatabase returns array data stored in notifications table [OK]
Common Mistakes:
  • Expecting a string instead of array
  • Thinking only one key is saved
  • Confusing toMail with toDatabase
4. Identify the error in this Laravel notification class snippet for sending SMS via Nexmo:
public function via($notifiable) {
    return ['nexmo'];
}

public function toNexmo($notifiable) {
    return (new NexmoMessage)->content('Hello!');
}
medium
A. Method name should be toSms, not toNexmo
B. The content method is not valid for NexmoMessage
C. The via method should return 'sms' instead of 'nexmo'
D. NexmoMessage class is not imported or aliased

Solution

  1. Step 1: Check method names and channel names

    Using 'nexmo' in via and method toNexmo is correct for SMS via Nexmo.
  2. Step 2: Verify class usage

    The NexmoMessage class must be imported with use statement or aliased; missing import causes error.
  3. Final Answer:

    NexmoMessage class is not imported or aliased -> Option D
  4. Quick Check:

    Missing import causes class not found error [OK]
Hint: Always import notification channel classes before use [OK]
Common Mistakes:
  • Renaming toNexmo incorrectly
  • Returning wrong channel name in via
  • Misusing content method on NexmoMessage
5. You want to send a Laravel notification via mail, database, and SMS (Nexmo). Which of the following via method implementations correctly sets all three channels?
hard
A. return ['mail', 'database', 'nexmo'];
B. return ['mail', 'database', 'sms'];
C. return ['MailChannel', 'DatabaseChannel', 'NexmoChannel'];
D. return ['mail', 'db', 'nexmo'];

Solution

  1. Step 1: Recall correct channel names for Laravel notifications

    Laravel uses lowercase strings 'mail', 'database', and 'nexmo' to specify channels.
  2. Step 2: Compare options for correct channel names

    return ['mail', 'database', 'nexmo']; uses correct channel names; others use incorrect or non-existent names.
  3. Final Answer:

    return ['mail', 'database', 'nexmo']; -> Option A
  4. Quick Check:

    Use exact channel strings in via array [OK]
Hint: Use exact lowercase channel names: mail, database, nexmo [OK]
Common Mistakes:
  • Using 'sms' instead of 'nexmo'
  • Using class names instead of strings
  • Using shorthand like 'db' instead of 'database'