Implementing Novu Notifications with Laravel: A Step-by-Step Guide
Tags: laravel, php, novu, womenintech
Published: 2024-03-20 17:45:33

**Hello Artisan,**

Today’s blog is about how to use Novu notifications. It provides a vast range of SDKs and Server Libraries. However, we will be using Laravel SDK and installing it in our laravel application. 

I hope you have read part one and have an idea of how to use Novu notification. If not, here is a link [Part 1: Simplify Communication with NOVU: A Developer's Guide](https://dev.to/snehalkadwe/simplify-communication-with-novu-a-developers-guide-5e8d). Make sure you read it to have a basic idea of what exactly the Novu does.

A quick overview of what we are learning in this blog post. 

1. Create project using laravel (v10)
2. Installation on Novu Laravel SDK
3. Usage of Novu notification

So let’s begin with the installation process.

**Step 1:** Create a laravel project using the following command and run a migration
**Note:** Create a migration to add `phone` field in users table
```bash
composer create-project laravel/laravel:^10.0 novu-laravel-app
//Run the migration
php artisan migrate
```
 
a. Create a dummy users using seeder to do that create a `UserSeeder`.

```php
php artisan make:seeder UserSeeder
```
b. Open `UserSeeder.php` file, add the given code and run the command. It will create five (5) users in our application.

```php
\App\Models\User::factory(5)->create();

php artisan db:seed --class=UserSeeder
```

 **Step 2:** Install novu-laravel SDK using composer.
```php
composer require novu/novu-laravel
```

You can publish the configuration file using this command:
```bash
php artisan vendor:publish --tag="novu-laravel-config"
```

**Step 3:** Usage of Novu notification in laravel

a. Create a `UserController` where we take all the users and send test `Email` and `SMS` to each of them.

```php 
php artisan make:controller UserController
```

b. Open `UserController` to add a snippet of Novu workflow which we created in our last blog post. 

- We can use `Novu` facade or `novu()` helper to trigger the event.

```php
use Novu\Laravel\Facades\Novu;
use App\Models\User;

public function sendNotification (Request $request) {
     $users = User::select('id', 'name', 'email', 'phone')
                ->get();
     foreach($users as $user) {
         // using Novu facade
        $response = Novu::triggerEvent([
           'name' => '<WORKFLOW_TRIGGER_IDENTIFIER_FROM_DASHBOARD>',
           'payload' => ['username' => $user->name],
           'to' => [
              'subscriberId' => '<SUBSCRIBER_ID_FROM_DASHBOARD>',
              'email' => $user->name,
              'phone' => $user->phone
           ]
        ])->toArray();
      }
}
```
- Make sure you have added this route in `web.php`

```php
Route::post('send-notification', [UserController::class, 'sendNotification'])->name('sendNotification');
```
To get the `WORKFLOW_TRIGGER_IDENTIFIER_FROM_DASHBOARD` go to the dashboard, click on `Get Snippet` and copy the `name` parameter. Refer to SS for a clear view.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l5ky90utzd54fv7pounx.png)

To get a `subscriberId` go to the left side click on the `Subscriber` menu and copy the subscription id.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3zh6g1lhpt6c5jnvo9q6.png)

Congratulations! You've now successfully integrated Novu notification infrastructure into your Laravel application. Feel free to explore its capabilities for other communication channels, and for further guidance, refer to Novu's comprehensive documentation for detailed instructions. [Novu Documentation](https://docs.novu.co/getting-started/introduction) 

Furthermore, you can explore additional details on my GitHub repository, where I've crafted a project named the **Daily Quote Application.**

**Overview of Project:**

- In this project, users can subscribe to the **Daily Quotes Application.**
- Upon subscription, users will receive a welcome email, while the project admin will be notified via email and SMS.
- Subscribed users will receive a daily quote via email.
- To facilitate this, I've developed a command which has been scheduled to run daily at a specific time.
**[Github Project Link](https://github.com/snehalkadwe/daily-quotes-app)**

Hope you find this article useful! 😊 Thanks for reading!

Happy Coding!!
Happy Reading!!
:unicorn: :heart: