0
0
Laravelframework~30 mins

Creating notifications in Laravel - Try It Yourself

Choose your learning style9 modes available
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
Need a 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
Need a 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
Need a 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
Need a hint?

Review all parts to ensure the notification class is complete and correct.