1

error enter image description here

I am trying to send notifications of the event when some likes and comment on his post, notifications for comments and likes working here is my notification class. i have error in my CommentController if ($event->user_id != $comment->user_id)

class NewCommentEvent extends Notification
{

    use Queueable;
    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,
            'event' => Event::find($this->comment->event_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 [
            //
        ];
    }
}

My controller function code for notifications on comments

 public function store(CommentRequest $request)
    {


        $event = Event::findOrFail($request->event_id);


        Comment::create([
            'comment' => $request->comment,
            'user_id' => Auth::id(),
            'event_id' => $event->id
        ]);

        if ($event->user_id != $comment->user_id) {
            $user = User::find($event->user_id);
            $user->notify(new NewCommentEvent($comment));
        }



        Toastr::success('Comment post with success','', ["positionClass" => "toast-top-center"]);
        return redirect()->back();
    }

my CommenRequest

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Auth;

class CommentRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return Auth::check();
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'comment' => 'required|max:2000',
        ];
    }
}
bokino12
  • 317
  • 5
  • 13

3 Answers3

2

In your controller: the variable $comment is not defined.

From Laravel docs:

The create method returns the saved model instance.

so the solution is:

$comment = Comment::create([
            'comment' => $request->comment,
            'user_id' => Auth::id(),
            'event_id' => $event->id
        ]);
Usama
  • 884
  • 10
  • 24
0

you have not defined your $comment, you just created a comment. This is throwing the error

$comment = Comment::create([
     .
     .
]);

This will fix your issue

athulpraj
  • 1,547
  • 1
  • 13
  • 24
0

The error message was clear. $comment is undefined. Replace your controller code with the follow:

public function store(CommentRequest $request)
    {


    $event = Event::findOrFail($request->event_id);

   // defined comment here
    $comment = Comment::create([
        'comment' => $request->comment,
        'user_id' => Auth::id(),
        'event_id' => $event->id
    ]);

    if ($event->user_id != $comment->user_id) {
        $user = User::find($event->user_id);
        $user->notify(new NewCommentEvent($comment));
    }



    Toastr::success('Comment post with success','', ["positionClass" => "toast-top-center"]);
    return redirect()->back();
}
Elisha Senoo
  • 3,489
  • 2
  • 22
  • 29