I’m working with Laravel 8 to make a forum and now I wanted to send a notification to a user who has asked a question whenever someone answered that question.
So in order to do that, I added this to the Controller:
public function postAnswer(Request $request, $question, $asker) { $validate_data = $request->validate([ 'answer' => 'required', ]); $answer = Answer::create([ 'answer' => $request->answer, 'user_id' => auth()->user()->id, 'question_id' => $question, ]); $asker->notify(new RepliedToThread($question)); return back(); }
So the $question
is the question id and $asker
is the user id of the person who has asked the question.
But now I get this error:
Call to a member function notify() on string
Now the interesting part is that, when I use auth()->user()->notify(new RepliedToThread($question));
, it is working fine!
But I don’t want to notify any authenticated user, I need to notify the person who has asked question, that’s why I tried passding $asker
…
So if you know how to solve this question, please let me know…
I would really appreciate any idea or suggestion from you guys.
Thanks in advance.
Blade:
<form action="{{ route('questions.answers', ['question' => $show->id, 'asker' => $show->user->id]) }}" method="POST" enctype="multipart/form-data"> @csrf <div class="mb-4"> <textarea name="answer" id="answer" class="form-control BSinaBold" rows="7" placeholder="Write down answer"></textarea> @error('answer') <div class="text-red-500 mt-2 text-sm"> {{ $message }} </div> @enderror </div> </br> <button type="submit" class="btn btn-primary">Send</button> </form>
Advertisement
Answer
The problem is that asker
is just an id, you should take the other information of the asker
from the user table and after that pass your notification to the asker
.
$asker = User::findOrFail($asker); $asker->notify(new RepliedToThread($question));