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
Recall & Review
beginner
What is the purpose of Laravel notifications?
Laravel notifications provide a simple way to send alerts or messages to users via different channels like email, SMS, or database.
Click to reveal answer
beginner
How do you create a new notification class in Laravel?
Use the artisan command: <code>php artisan make:notification NotificationName</code>. This creates a new notification class in the <code>app/Notifications</code> folder.
Click to reveal answer
intermediate
What method do you define to specify the delivery channels for a Laravel notification?
Define the via() method in your notification class. It returns an array of channels like ['mail', 'database'].
Click to reveal answer
beginner
How do you send a notification to a user in Laravel?
Call the notify() method on the user instance with the notification object, like $user->notify(new NotificationName());.
Click to reveal answer
intermediate
What is the purpose of the toArray() method in a Laravel notification?
The toArray() method defines the data stored in the database when using the database notification channel.
Click to reveal answer
Which artisan command creates a new notification class?
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'