Skip to content

Commit

Permalink
Use Notification Email instead of Email.(Optional)
Browse files Browse the repository at this point in the history
  • Loading branch information
subhadipghorui committed Jan 23, 2021
1 parent f85b0db commit 8d89d2c
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 2 deletions.
12 changes: 10 additions & 2 deletions app/Http/Controllers/Admin/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
use App\Category;
use App\Http\Controllers\Controller;
use App\Mail\NewPost;
use App\Notifications\NewPostNotify;
use App\Post;
use App\User;
use Brian2694\Toastr\Facades\Toastr;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Notification;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Intervention\Image\Facades\Image;
Expand Down Expand Up @@ -87,7 +89,10 @@ public function store(Request $request)
if($post->status){
$users = User::all();
foreach($users as $user){
Mail::to($user->email)->queue(new NewPost($post));
// Mail::to($user->email)->queue(new NewPost($post));
// Use notification to notify
Notification::route('mail', $user->email)
->notify(new NewPostNotify($post));
}
}
$tags = [];
Expand Down Expand Up @@ -192,7 +197,10 @@ public function update(Request $request, $id)
if($post->status){
$users = User::all();
foreach($users as $user){
Mail::to($user->email)->queue(new NewPost($post));
// Mail::to($user->email)->queue(new NewPost($post));
// Use Notification
Notification::route('mail', $user->email)
->notify(new NewPostNotify($post));
}
}
// delete old tags
Expand Down
66 changes: 66 additions & 0 deletions app/Notifications/NewPostNotify.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Notification;
use Illuminate\Notifications\Messages\MailMessage;

class NewPostNotify extends Notification implements ShouldQueue
{
use Queueable;
public $post;

/**
* Create a new notification instance.
*/
public function __construct($post)
{
$this->post = $post;
}

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

/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
*
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage())
->subject('New Post Availabel')
->greeting('Hello Subscriber !')
->line('There is a new Post. Hope you will Like it :)')
->line('Post Title : '.$this->post->title)
->line('Author : '.$this->post->user->name)
->action('View Post', url(route('post', $this->post->slug)))
->line('Thank you.');
}

/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
*
* @return array
*/
public function toArray($notifiable)
{
return [
];
}
}

0 comments on commit 8d89d2c

Please sign in to comment.