Share this:

Today,we are going to learn Laravel 10 Notification: Create Notifications in Laravel 10. Laravel is an open-source PHP framework that makes web development a breeze. With its expressive syntax and powerful tools, it enables developers to create elegant and feature-rich applications.

One of the many powerful features Laravel offers is its built-in notification system. In this blog post, we will explore Laravel 10 Notification and learn how to create notifications in Laravel 10 step by step.

Table of Contents

  1. What Are Laravel Notifications?
  2. Setting Up the Environment
  3. Creating a New Laravel 10 Project
  4. Creating a Notification
  5. Setting Up the Database and Mail Configuration
  6. Sending Notifications
  7. Customizing the Notification Template
  8. Queuing Notifications
  9. Testing
  10. Conclusion

What Are Laravel Notifications?

Laravel Notifications provide a simple and convenient way to send notifications across different channels, including email, SMS, and Slack, to name a few.

This feature allows developers to keep users informed about important updates or changes within the application.

Notifications can be sent to users on multiple channels, which can be customized depending on the application’s requirements.

Setting Up the Environment

To start, you’ll need to have the following tools and software installed on your system:

  • PHP 8.1 or higher
  • Composer (dependency manager for PHP)
  • Laravel Installer
  • A text editor or IDE (such as Visual Studio Code, Sublime Text, or PHPStorm)
  • A database system (MySQL, PostgreSQL, SQLite, or SQL Server)

Creating a New Laravel 10 Project

First, open your terminal (or command prompt) and run the following command to create a new Laravel project:

composer create-project --prefer-dist laravel/laravel laravel10-notification
Creating a New Laravel 10 Project
Creating-a-New-Laravel-10-Project

Once the installation is complete, navigate to the project directory:

cd laravel10-notification

Now, launch the Laravel development server using the following command:

php artisan serve

You can now access your new Laravel 10 project in your web browser at http://localhost:8000.

Creating a Notification

To create a notification in Laravel, we will use the Artisan command-line tool. In your terminal, run the following command to create a new notification class:

php artisan make:notification MyNotification
Creating a Notification
Creating a Notification

This will create a new file named MyNotification.php in the app/Notifications directory. Open this file in your text editor or IDE and examine its contents.

By default, the toMail method is provided, which is responsible for sending notifications via email. You can add other channels and methods as required for your application.

Setting Up the Database and Mail Configuration

To store and send notifications, you need to configure your database and mail settings. Open the .env file in your project’s root directory and update the following lines with your database and mail server credentials:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database_name
DB_USERNAME=your_database_user
DB_PASSWORD=your_database_password

MAIL_MAILER=smtp
MAIL_HOST=your_mail_host
MAIL_PORT=your_mail_port
MAIL_USERNAME=your_mail_username
MAIL_PASSWORD=your_mail_password
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=your_mail_from_address
MAIL_FROM_NAME="${APP_NAME}"

Install the Laravel UI package using Composer:

composer require laravel/ui

Generate the authentication scaffolding:

php artisan ui bootstrap --auth

Next, run the following command to migrate the default Laravel tables:

php artisan migrate
Setting Up the Database and Mail Configuration
Setting Up the Database and Mail Configuration

Sending Notifications

To send notifications, you need to use the notify method on a notifiable object. Typically, this will be an instance of the User model. In this example, let’s assume you want to send a notification to a user when they register on your website.

First, open the app/Http/Controllers/Auth/RegisterController.php file and add the following import statement at the top:

use App\Notifications\MyNotification;

Next, locate the create method within the RegisterController class, and add the following line after the user is created:

$user->notify(new MyNotification($user));

Your create method should now look like this:

protected function create(array $data)
{
    $user = User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => Hash::make($data['password']),
    ]);

    $user->notify(new MyNotification($user));

    return $user;
}
Sending Notifications
Sending Notifications

Now, whenever a new user is registered, they will receive a notification via email.

Customizing the Notification Template

To customize the email notification template, open the app/Notifications/MyNotification.php file and update the toMail method.

You can use the line and action methods to add content and a call-to-action button to the email, respectively.

For example, you can customize the notification like this:

public function toMail($notifiable)
{
    return (new MailMessage)
        ->subject('Welcome to Laravel 10 Notification')
        ->greeting('Hello, ' . $notifiable->name)
        ->line('Thank you for registering on our website.')
        ->action('Visit Website', url('/'))
        ->line('If you have any questions, feel free to contact us.');
}
Customizing the Notification Template
Customizing the Notification Template

Queuing Notifications

To improve performance, you can queue notifications instead of sending them immediately. To do this, implement the ShouldQueue interface and use the Queueable trait in your notification class.

First, update the MyNotification class by adding the following import statements at the top:

use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Notification;
use Illuminate\Bus\Queueable;

Next, update the class definition to implement the ShouldQueue interface and use the Queueable trait:

class MyNotification extends Notification implements ShouldQueue
{
    use Queueable;
    // ...
}

Now, your notifications will be queued and processed in the background, improving the overall performance of your application.

Testing

Vite development server, run the following command in your terminal:

npm install
npm run dev

This will start the Vite development server.

In a separate terminal, navigate to your project’s root directory and start the Laravel backend server:

php artisan serve

This will start the Laravel development server on port 8000 by default.

http://127.0.0.1:8000/

Register for new user and check the mail.

Laravel 10 Notification: Create Notifications in Laravel 10
Laravel 10 Notification: Create Notifications in Laravel 10

Conclusion

In this blog post, we explored the powerful Laravel 10 Notification feature and demonstrated how to create and send notifications in Laravel 10. With this knowledge, you can now easily implement notifications in your Laravel applications to keep users informed and engaged.

Remember to customize your notifications based on your application’s requirements and consider using different notification channels as needed. Additionally, don’t forget to take advantage of Laravel’s built-in queuing system to enhance the performance and user experience of your application.

Share this:

Categorized in: