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',
        ];
    }
}
1
  • And what is your question? Is there any problem with all that code? Commented Jun 23, 2018 at 8:21

3 Answers 3

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
        ]);
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.