DEV Community

Ariyo Ayomide
Ariyo Ayomide

Posted on

A Complete Guide to Laravel Database Notifications.

We'll learn how to send a notification to database in Laravel 8 today. We all know that the Laravel mechanism is getting stronger by the day. The laravel notification system not only works with database, but also with email, broadcasts, sms, slack, markdown, and more.

Notifications are a type of communication that is sent to a user to provide them with important information, events, or to prompt them to take action in the program.

Before entering into the database notification procedure, make sure you've completed the following steps:

  1. Install Laravel Project.
  2. Set Up Database Connection in your .env file.

If you've accomplished the first two steps, you're ready to begin learning about how laravel database notifications operate.

Create Notification Database Table

In this phase, you'll learn how to establish a database table in Laravel to store the notification.

To create a notification table in your database, run the command below.

php artisan notifications:table
Enter fullscreen mode Exit fullscreen mode

The aforementioned command resulted in the creation of a notification file, which has the following path:

database/migrations/timestamp_create_notifications_table.php

The contents of the above-mentioned directory file are listed below.

public function up()
{
    Schema::create('notifications', function (Blueprint $table) {
        $table->uuid('id')->primary();
        $table->string('type');
        $table->morphs('notifiable');
        $table->text('data');
        $table->timestamp('read_at')->nullable();
        $table->timestamps();
    });
}
Enter fullscreen mode Exit fullscreen mode

Then, to migrate the notification table, run the command.

php artisan migrate
Enter fullscreen mode Exit fullscreen mode

Create Notification in Laravel 8.
Then, to build the notification, use the laravel artisan command and run the following command to create "Testing Notification."

php artisan make:notification TestingNotification
Enter fullscreen mode Exit fullscreen mode

The command above created a new file and folder with the path app/Notifications/TestingNotification.php.

<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Notification;
class TestingNotification extends Notification
{
    use Queueable;
    private $testing;
    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct($testing)
    {
        $this->testing = $testing;

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['database'];
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            'testing' => $this->testing
        ];
    }
}
Enter fullscreen mode Exit fullscreen mode

Create Testing Controller

We'll develop a controller called TestingController. To make a controller, run the command below.

php artisan make:controller TestingController
Enter fullscreen mode Exit fullscreen mode

The core logic for sending notifications will be kept in this controller. In comparison to other capabilities, the notification mechanism we're working on is minimal.

Add the following code to the app/Http/Controllers/TestingController.php file to make this controller functional.

  public function store()
    {
     $testNotification = User::first();
     $testNotification->notify(new TestingNotification(900));
    dd($testNotification->notifications);
    }
Enter fullscreen mode Exit fullscreen mode

Creat Notification Route

The Route method should be used to declare the http method in general. Get solves our problem by passing the route url name, as well as the controller class and sub-method of the controller.

To construct the route, add the following code to routes/web.php.

<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\TestingController;

    Route::post('test-notification', [App\Http\Controllers\TestingController::class, 'store']);

Enter fullscreen mode Exit fullscreen mode

Once you've completed everything, use the command below to launch the Laravel app in the browser.

php artisan serve
Enter fullscreen mode Exit fullscreen mode

The following path can be used to test the notification system.

http://127.0.0.1:8000/test-notification

On your computer, you should see the image below.

The response to dd($testNotification->notifications);

If you look at your database's notification table, you should see something like this.

Image of the stored notification in our database

In conclusion
By now, I'm sure we're on the same page, why using laravel Database Notification.

That's all folks.

If you have any doubt ask me in the comments section and I'll try to answer as soon as possible.

If you love the article follow me on Twitter: @ariyoraphael1

If you are the Linkedin type, let's connect: https://www.linkedin.com/in/ariyo-raphael-023822168/

Have an awesome day ahead 😀!

Top comments (0)