0

I am trying to send notifications to the owner of the post when some like and comment on his post, notifications for comments are working but when I do the same work for likes it is not working.

here is my notification class

<?php

namespace App\Notifications;

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

class show_notification extends Notification
{
    use Queueable;

    protected $comment;
    protected $likes;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct($comment,$likes)
    {
        $this->comment = $comment;
        $this->likes = $likes;
    }

    /**
     * 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,
            'likes' => $this->likes,
            'user' => auth()->user()
            //'repliedTime' => Carbon::now()
        ];
    }

    public function toArray($notifiable)
    {
        return [
            //
        ];
    }
}

My controller function code for notifications on comments

 $id = $comment->post_id;
 $getuser = Post::where('id', $id)->first();
 $userid  = $getuser->user_id;
 if ($userid != $user_id ){
     $user = User::where('id', $userid)->first();
     $user->notify(new show_notification($comment));
 }

My controller function code for notifications on likes

 $id = $likes->post_id;
 $getuser = Post::where('id', $id)->first();
 $userid  = $getuser->user_id;
 if ($userid != $user_id ){
     $user = User::where('id', $userid)->first();
     $user->notify(new show_notification($likes));
 }
linktoahref
  • 7,812
  • 3
  • 29
  • 51
DevTaabi
  • 77
  • 1
  • 3
  • 13
  • 6
    The `show_notifications` class accepts two parameters in it's constructor. The first is the `$comment` and the second one is the `$like`. The quickest (and dirtiest) solution is to changes line when you send a notification for a like to the user to this: `$user->notify(new show_notification(null, $likes));`. But you should split your notification into two notifications. So that you can send different kind of notification for a comment or a like. – Dees Oomens Jul 22 '17 at 14:48
  • This quickest solution doesn't work for me, is there any other solution ? – DevTaabi Jul 22 '17 at 14:53
  • Solution proposed by @Dees040 will work perfectly. If it doesn't, you have something wrong in your code – piotr Jul 22 '17 at 15:11
  • Its working .. Thanks ! – DevTaabi Jul 22 '17 at 15:23

0 Answers0