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
Creating notifications
📖 Scenario: You are building a simple Laravel app that sends notifications to users when they receive a new message.
🎯 Goal: Build a Laravel notification class that sends a notification via the database channel with a custom message.
📋 What You'll Learn
Create a Laravel notification class named NewMessageNotification
Add a public property $message to hold the notification text
Configure the notification to use the database channel
Format the notification data to include the message property
💡 Why This Matters
🌍 Real World
Notifications keep users informed about important events like new messages, updates, or alerts in web applications.
💼 Career
Understanding Laravel notifications is essential for backend developers building interactive and user-friendly applications.
Progress0 / 4 steps
1
Create the notification class with a message property
Create a Laravel notification class named NewMessageNotification with a public property $message set via the constructor.
Laravel
Hint
Remember to declare the $message property as public and assign it in the constructor.
2
Set the notification delivery channel
Add a via method to the NewMessageNotification class that returns an array with the string 'database' to specify the delivery channel.
Laravel
Hint
The via method tells Laravel which channels to use for sending the notification.
3
Format the notification data for the database
Add a toDatabase method to the NewMessageNotification class that returns an array with a key 'message' set to the $message property.
Laravel
Hint
The toDatabase method formats the data saved in the notifications table.
4
Complete the notification class
Ensure the NewMessageNotification class includes the public $message property, the constructor, the via method returning ['database'], and the toDatabase method returning the message array.
Laravel
Hint
Review all parts to ensure the notification class is complete and correct.
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
Step 1: Understand the role of notifications
Notifications are designed to keep users informed about events or updates in the application.
Step 2: Compare with other options
Creating tables, authentication, and performance optimization are unrelated to notifications.
Final Answer:
To inform users about important events or updates -> Option A
Quick Check:
Notifications = Inform users [OK]
Hint: Notifications inform users about events [OK]
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
Step 1: Check how notifications are sent
The notify() method requires a notification instance, so InvoicePaid must be created with new InvoicePaid().
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.
Final Answer:
InvoicePaid should be instantiated with new keyword -> Option A
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
Step 1: Recall the via() method role
In Laravel notifications, the via() method defines which channels the notification uses.
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.
Final Answer:
public function via($notifiable) { return ['mail', 'database']; } -> Option C
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'