Laravel allows us to send notifications to users through different channels (SMS, mail, etc.). In this post, we will add notifications to our project, but will be a type of notifications that will be saved in the database and will be shown in a tab in the header.
Create the table in the database for the notifications, Laravel provide us one command to do this:
1 |
php artisan notifications:table |
Execute it:
1 |
php artisan migrate |
Create the notification file:
1 |
php artisan make:notification NewCommentPost |
In this case, it will be a notification that will be launched when there is a new comment in the posts and will be sent to the creator of the post, saying him that a user has commented on one of his posts.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
<?php namespace App\Notifications; use Illuminate\Notifications\Notification; use App\Post; use App\User; class NewCommentPost extends Notification { protected $comment; /** * Create a new notification instance. * * @return void */ public function __construct($comment) { $this->comment = $comment; } /** * 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 toDatabase($notifiable) { return [ 'comment' => $this->comment, 'post' => Post::find($this->comment->post_id), 'user' => User::find($this->comment->user_id) ]; } /** * Get the array representation of the notification. * * @param mixed $notifiable * @return array */ public function toArray($notifiable) { return [ // ]; } } |
- In the via function we indicated the type, we could add more than one.
- In the toDatabase function we add the data that we want to access when we show the notification, we will see how now.
You can find more info in the documentation.
Open the CommentsController.php and modify the store function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
use App\Notifications\NewCommentPost; ... public function store(CommentRequest $request) { $post = Post::findOrFail($request->post_id); $comment = Comment::create([ 'body' => $request->body, 'user_id' => Auth::id(), 'post_id' => $post->id ]); if ($post->user_id != $comment->user_id) { $user = User::find($post->user_id); $user->notify(new NewCommentPost($comment)); } return redirect()->route('posts.show', $post->id); } |
That line is the one that sends the notification to the specified user.
It could also be added that once a user comments on a post, when that post has a new comment, this user is notified.
Now, we will add the code to show the notifications. Add this code in the header when the user is logged in:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> Notifications <span class="badge">{{count(Auth::user()->unreadNotifications)}}</span> </a> <ul class="dropdown-menu" role="menu"> <li> @foreach (Auth::user()->unreadNotifications as $notification) <a href="{{ route('posts.show', $notification->data['post']['id']) }}"><i>{{ $notification->data["user"]["name"] }}</i> has commented in <b>{{ $notification->data["post"]["title"] }}</b></a> @endforeach </li> </ul> </li> |
And we should see something like this:
It could easily be added with jQuery and Ajax that when you click on the Notifications button, the notifications in the list are marked as read.
1 2 |
$user = Auth::user(); $user->unreadNotifications->markAsRead(); |
-