<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Ariyo Ayomide </title>
    <description>The latest articles on Forem by Ariyo Ayomide  (@softraph).</description>
    <link>https://forem.com/softraph</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F546156%2F1492ca06-274a-43eb-b62a-5f7ab1541d47.jpeg</url>
      <title>Forem: Ariyo Ayomide </title>
      <link>https://forem.com/softraph</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/softraph"/>
    <language>en</language>
    <item>
      <title>A Complete Guide to Laravel Database Notifications.</title>
      <dc:creator>Ariyo Ayomide </dc:creator>
      <pubDate>Fri, 11 Mar 2022 18:45:03 +0000</pubDate>
      <link>https://forem.com/softraph/a-complete-guide-to-laravel-database-notifications-2c76</link>
      <guid>https://forem.com/softraph/a-complete-guide-to-laravel-database-notifications-2c76</guid>
      <description>&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;Before entering into the database notification procedure, make sure you've completed the following steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Install Laravel Project.&lt;/li&gt;
&lt;li&gt;Set Up Database Connection in your .env file.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If you've accomplished the first two steps, you're ready to begin learning about how laravel database notifications operate.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Create Notification Database Table&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In this phase, you'll learn how to establish a database table in Laravel to store the notification.&lt;/p&gt;

&lt;p&gt;To create a notification table in your database, run the command below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;php artisan notifications:table
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The aforementioned command resulted in the creation of a notification file, which has the following path:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;database/migrations/timestamp_create_notifications_table.php&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
The contents of the above-mentioned directory file are listed below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public function up()
{
    Schema::create('notifications', function (Blueprint $table) {
        $table-&amp;gt;uuid('id')-&amp;gt;primary();
        $table-&amp;gt;string('type');
        $table-&amp;gt;morphs('notifiable');
        $table-&amp;gt;text('data');
        $table-&amp;gt;timestamp('read_at')-&amp;gt;nullable();
        $table-&amp;gt;timestamps();
    });
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, to migrate the notification table, run the command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;php artisan migrate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Create Notification in Laravel 8.&lt;br&gt;
Then, to build the notification, use the laravel artisan command and run the following command to create "Testing Notification."&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;php artisan make:notification TestingNotification
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The command above created a new file and folder with the path app/Notifications/TestingNotification.php.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?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-&amp;gt;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' =&amp;gt; $this-&amp;gt;testing
        ];
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Create Testing Controller&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We'll develop a controller called TestingController. To make a controller, run the command below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;php artisan make:controller TestingController
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;Add the following code to the app/Http/Controllers/TestingController.php file to make this controller functional.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  public function store()
    {
     $testNotification = User::first();
     $testNotification-&amp;gt;notify(new TestingNotification(900));
    dd($testNotification-&amp;gt;notifications);
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Creat Notification Route&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;To construct the route, add the following code to routes/web.php.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\TestingController;

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

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Once you've completed everything, use the command below to launch the Laravel app in the browser.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;php artisan serve
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;The following path can be used to test the notification system.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code&gt;http://127.0.0.1:8000/test-notification&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;On your computer, you should see the image below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fruaq0l8tuc2nf2mkvail.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fruaq0l8tuc2nf2mkvail.png" alt="The response to dd($testNotification-&amp;gt;notifications);"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you look at your database's notification table, you should see something like this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3aiwtp6xabhvwtc7llf1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3aiwtp6xabhvwtc7llf1.png" alt="Image of the stored notification in our database"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In conclusion&lt;br&gt;
By now, I'm sure we're on the same page, why using laravel Database Notification.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That's all folks.&lt;/p&gt;

&lt;p&gt;If you have any doubt ask me in the comments section and I'll try to answer as soon as possible.&lt;/p&gt;

&lt;p&gt;If you love the article follow me on Twitter: @AriyoRaphael1&lt;/p&gt;

&lt;p&gt;If you are the Linkedin type, let's connect: &lt;a href="https://www.linkedin.com/in/ariyo-raphael-023822168/" rel="noopener noreferrer"&gt;https://www.linkedin.com/in/ariyo-raphael-023822168/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Have an awesome day ahead 😀!&lt;/p&gt;

</description>
      <category>database</category>
      <category>laravel</category>
      <category>php</category>
    </item>
  </channel>
</rss>
