Bird
Raised Fist0
Laravelframework~20 mins

Creating notifications in Laravel - Practice Exercises

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 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!');
    }
}
AThe email subject will be 'Thank you for joining us!'.
BThe email subject will be 'Welcome to Our App'.
CThe email will have no subject.
DThe email subject will be the user's name.
Attempts:
2 left
💡 Hint
Look at the subject() method inside the toMail() function.
❓ state_output
intermediate
2: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);
Anull
B['nexmo']
C[] (empty array)
D['mail', 'database']
Attempts:
2 left
💡 Hint
Check the condition inside via() and the value of prefers_sms.
📝 Syntax
advanced
2:00remaining
Which option causes a syntax error in this Laravel notification?
Identify which notification class code snippet will cause a syntax error.
Apublic function toMail($notifiable) { return new MailMessage->line('Hello'); }
Bpublic function toMail($notifiable) { return (new MailMessage())->line('Hello'); }
Cpublic function toMail($notifiable) { return (new MailMessage)->line('Hello'); }
Dpublic function toMail($notifiable) { return (new MailMessage)->subject('Hi')->line('Hello'); }
Attempts:
2 left
💡 Hint
Check how the new keyword is used with method calls.
🔧 Debug
advanced
2: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.');
    }
}
AThe via() method does not include 'mail' channel.
BThe toMail() method is missing a return statement.
CThe MailMessage class is not imported.
DThe notification class does not extend Notification.
Attempts:
2 left
💡 Hint
Check the channels returned by via() method.
🧠 Conceptual
expert
3:00remaining
Which option correctly describes Laravel notification broadcasting?
In Laravel, when using notification broadcasting, which statement is true?
ABroadcast notifications require the notifiable model to implement the ShouldBroadcast interface.
BBroadcast notifications are sent automatically without defining a toBroadcast method.
CBroadcast notifications require the notification class to implement ShouldBroadcast and define toBroadcast method.
DBroadcast notifications can only be sent via mail channel.
Attempts:
2 left
💡 Hint
Think about the interface and method needed for broadcasting notifications.

Practice

(1/5)
1. What is the primary purpose of notifications in Laravel?
easy
A. To inform users about important events or updates
B. To create database tables automatically
C. To handle user authentication
D. To optimize application performance

Solution

  1. Step 1: Understand the role of notifications

    Notifications are designed to keep users informed about events or updates in the application.
  2. Step 2: Compare with other options

    Creating tables, authentication, and performance optimization are unrelated to notifications.
  3. Final Answer:

    To inform users about important events or updates -> Option A
  4. Quick Check:

    Notifications = Inform users [OK]
Hint: Notifications inform users about events [OK]
Common Mistakes:
  • Confusing notifications with database migrations
  • Thinking notifications handle authentication
  • Assuming notifications improve performance directly
2. Which artisan command is used to create a new notification class in Laravel?
easy
A. php artisan make:notify NotificationName
B. php artisan generate:notification NotificationName
C. php artisan create:notification NotificationName
D. php artisan make:notification NotificationName

Solution

  1. Step 1: Recall the artisan command for notifications

    The correct command to create a notification class is php artisan make:notification followed by the notification name.
  2. Step 2: Identify incorrect commands

    Other commands like make:notify, create:notification, or generate:notification do not exist in Laravel.
  3. Final Answer:

    php artisan make:notification NotificationName -> Option D
  4. Quick Check:

    Use make:notification to create notifications [OK]
Hint: Use 'make:notification' with artisan [OK]
Common Mistakes:
  • Using 'make:notify' instead of 'make:notification'
  • Confusing 'create' or 'generate' commands
  • Omitting the notification class name
3. Given this code snippet, what will be the result?
use App\Notifications\InvoicePaid;
$user = User::find(1);
$user->notify(new InvoicePaid($invoice));
medium
A. Throws an error because notify() is not a valid method
B. Sends an InvoicePaid notification to the user with ID 1
C. Creates a new user with an InvoicePaid notification
D. Deletes the user with ID 1 after notification

Solution

  1. Step 1: Understand the notify() method

    The notify() method sends the given notification instance to the user model.
  2. Step 2: Analyze the code behavior

    The code finds the user with ID 1 and sends them the InvoicePaid notification.
  3. Final Answer:

    Sends an InvoicePaid notification to the user with ID 1 -> Option B
  4. Quick Check:

    notify() sends notification to user [OK]
Hint: notify() sends notification to the user model [OK]
Common Mistakes:
  • Thinking notify() creates or deletes users
  • Assuming notify() is not a valid method
  • Confusing notification sending with user creation
4. Identify the error in this notification sending code:
$user = User::find(5);
$user->notify(InvoicePaid);
medium
A. InvoicePaid should be instantiated with new keyword
B. User::find should be User::get
C. notify() method does not exist on User model
D. User ID 5 does not exist

Solution

  1. Step 1: Check how notifications are sent

    The notify() method requires a notification instance, so InvoicePaid must be created with new InvoicePaid().
  2. Step 2: Verify other parts

    User::find(5) is correct to get a user by ID, and notify() exists on the User model. User existence is not guaranteed but not a syntax error.
  3. Final Answer:

    InvoicePaid should be instantiated with new keyword -> Option A
  4. Quick Check:

    Notifications need new instance [OK]
Hint: Always use 'new' to create notification instances [OK]
Common Mistakes:
  • Passing class name without instantiation
  • Using User::get instead of find for single user
  • Assuming notify() method is missing
5. You want to send a notification only via email and database channels. Which method should you define in your notification class to specify these channels?
hard
A. public function sendChannels() { return ['mail', 'database']; }
B. public function channels() { return ['email', 'db']; }
C. public function via($notifiable) { return ['mail', 'database']; }
D. public function notifyChannels() { return ['email', 'database']; }

Solution

  1. Step 1: Recall the via() method role

    In Laravel notifications, the via() method defines which channels the notification uses.
  2. Step 2: Check correct channel names and method signature

    The method must accept the $notifiable parameter and return an array with channel names like 'mail' and 'database'. Other method names or channel names are incorrect.
  3. Final Answer:

    public function via($notifiable) { return ['mail', 'database']; } -> Option C
  4. Quick Check:

    via() defines notification channels [OK]
Hint: Use via() method to list channels [OK]
Common Mistakes:
  • Using wrong method names like channels()
  • Using incorrect channel names like 'email' instead of 'mail'
  • Omitting the $notifiable parameter