Laravel 7/6 Notification Tutorial | Create Notification with Laravel 7/6

Laravel 7/6 Notification Tutorial | Create Notification with Laravel 7/6

In this post we will give you information about Laravel 7/6 Notification Tutorial | Create Notification with Laravel 7/6. Hear we will give you detail about Laravel 7/6 Notification Tutorial | Create Notification with Laravel 7/6And how to use it also give you demo for it if it is necessary.

Hi Guys,

In this tutorial, i will guide you how to send email notification in laravel 7/6. we will create laravel 7/6 notification to email address. we will send email to notify user using laravel 7/6 notification system.

Using laravel 6 notifications, you can send email, send sms, send slack message notification to user. in this example i give you very simple way to create first notification to send mail in laravel 6. we can easily create Notification by laravel artisan command. we can easily customization of notification like mail subject, mail body, main action etc. we almost require to use notification when we work on large amount of project like e-commerce. might be you need to send notification for payment receipt, order place receipt, invoice etc.

In this example we will create email notification and send it to particular user, than we saved to database. So, you need to follow few step to make basic example with notification.

Step 1: Install Laravel 6

I am going to explain step by step from scratch so, we need to get fresh Laravel 6 application using bellow command, So open your terminal OR command prompt and run bellow command:

composer create-project --prefer-dist laravel/laravel blog

Step 2: Create Database Table

In this step, we need to create “notifications” table by using laravel 5 artisan command, so let’s run bellow command:

php artisan notifications:table

php artisan migrate

Also see:Laravel 6 Generate PDF File Tutorial

Step 3: Create Notification

In this step, we need to create “Notification” by using laravel 5 artisan command, so let’s run bellow command, we will create MyFirstNotification.

php artisan make:notification MyFirstNotification

now you can see new folder will create as “Notifications” in app folder. You need to make following changes as like bellow class.

app/Notifications/MyFirstNotification.php

<?php

namespace AppNotifications;

use IlluminateBusQueueable;

use IlluminateNotificationsNotification;

use IlluminateContractsQueueShouldQueue;

use IlluminateNotificationsMessagesMailMessage;

class MyFirstNotification extends Notification

{

use Queueable;

private $details;

/**

* Create a new notification instance.

*

* @return void

*/

public function __construct($details)

{

$this->details = $details;

}

/**

* Get the notification's delivery channels.

*

* @param mixed $notifiable

* @return array

*/

public function via($notifiable)

{

return ['mail','database'];

}

/**

* Get the mail representation of the notification.

*

* @param mixed $notifiable

* @return IlluminateNotificationsMessagesMailMessage

*/

public function toMail($notifiable)

{

return (new MailMessage)

->greeting($this->details['greeting'])

->line($this->details['body'])

->action($this->details['actionText'], $this->details['actionURL'])

->line($this->details['thanks']);

}

/**

* Get the array representation of the notification.

*

* @param mixed $notifiable

* @return array

*/

public function toDatabase($notifiable)

{

return [

'order_id' => $this->details['order_id']

];

}

}

Step 4: Create Route

In this is step we need to create routes for sending notification to one user. so open your “routes/web.php” file and add following route.


routes/web.php

Route::get('send', 'HomeController@sendNotification');

Step 4: Create Controller

Here,we require to create new controller HomeController that will manage generatePDF method of route. So let’s put bellow code.

app/Http/Controllers/HomeController.php

<?php

namespace AppHttpControllers;

use IlluminateHttpRequest;

use AppUser;

use Notification;

use AppNotificationsMyFirstNotification;

class HomeController extends Controller

{

/**

* Create a new controller instance.

*

* @return void

*/

public function __construct()

{

$this->middleware('auth');

}

/**

* Show the application dashboard.

*

* @return IlluminateContractsSupportRenderable

*/

public function index()

{

return view('home');

}

public function sendNotification()

{

$user = User::first();

$details = [

'greeting' => 'Hi Artisan',

'body' => 'This is my first notification from ItSolutionStuff.com',

'thanks' => 'Thank you for using ItSolutionStuff.com tuto!',

'actionText' => 'View My Site',

'actionURL' => url('/'),

'order_id' => 101

];

Notification::send($user, new MyFirstNotification($details));

dd('done');

}

}

Now we are ready to send first notification to user. so let’s run our example so run bellow command for quick run:

php artisan serve

you can run following url:

http://localhost:8000/send

you can also send notification like this way:

$user->notify(new MyFirstNotification($details));

you can get sent notifications by following command:

Also see:Laravel 7/6 Socialite Login with Google Gmail Account

dd($user->notifications);

I hope it can help you….

Hope this code and post will helped you for implement Laravel 7/6 Notification Tutorial | Create Notification with Laravel 7/6. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

2  +  7  =  

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US