<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
use App\Http\Controllers\HomeController;
use App\Http\Controllers\ItemController;
Route::get('/', function () {
dd('Welcome to simple user route file.');
});
Auth::routes();
Route::get('/home', [HomeController::class, 'index'])->name('home');
Route::get('send', [HomeController::class,'sendNotification']);
Laravel 8/7 Notification Tutorial | Create Notification with Laravel 8/7
By Hardik Savani November 23, 2019 Category : LaravelPlayUnmuteLoaded: 1.17%FullscreenHi Guys,
In this tutorial, i will guide you how to send email notification in laravel 8/7/6. we will create laravel 8/7 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:
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
namespace App\Http\Controllers;
use App\Models\User;
use App\Notifications\MyFirstNotification;
use Notification;
class HomeController extends Controller {
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct() {
$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* @return \Illuminate\Contracts\Support\Renderable
*/
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');
}
}
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class MyFirstNotification extends Notification {
use Queueable;
/**
* Create a new notification instance.
*
* @return void
*/
private $details;
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 \Illuminate\Notifications\Messages\MailMessage
*/
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 toArray($notifiable) {
return [
'order_id' => $this->details['order_id'],
];
}
}
composer create-project --prefer-dist laravel/laravel blog
php artisan notifications:table
php artisan migrate
php artisan make:notification MyFirstNotification
<?php namespace App\Notifications; use Illuminate\Bus\Queueable;use Illuminate\Notifications\Notification;use Illuminate\Contracts\Queue\ShouldQueue;use Illuminate\Notifications\Messages\MailMessage; 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 \Illuminate\Notifications\Messages\MailMessage */ 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'] ]; }}
<?php namespace App\Http\Controllers; use Illuminate\Http\Request;use App\User;use Notification;use App\Notifications\MyFirstNotification; class HomeController extends Controller{ /** * Create a new controller instance. * * @return void */ public function __construct() { $this->middleware('auth'); } /** * Show the application dashboard. * * @return \Illuminate\Contracts\Support\Renderable */ 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'); } }