Reputation: 349
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', ]; } }
Upvotes: 1
Views: 1376
Reputation: 3594
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();
}
Upvotes: 0
Reputation: 944
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
]);
Upvotes: 2
Reputation: 1577
you have not defined your $comment, you just created a comment. This is throwing the error
$comment = Comment::create([
.
.
]);
This will fix your issue
Upvotes: 0