php - Laravel 8: Call to a member function notify() on string
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>
Answer
Solution:
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));
Answer
Solution:
Based on what you said, the problem is $asker
. This parameter should be an instance from User
model but the error implies it is string.
In your form change the form tag:
<form action="{{ route('questions.answers', ['question' => $show->id, 'asker' => $show->user]) }}" method="POST" enctype="multipart/form-data">
Remove the id
after user
.
Answer
Solution:
make sure your Answer.php
have use Notifiable;
imported
ref link https://laravel.com/docs/8.x/notifications#using-the-notifiable-trait
then only you can call notify()
function from Answer
model instance
Answer
Solution:
The problem is that $asker
is just a string, its just the id
value of the user.
I recommend that you use laravels route model binding. The passed parameters $querstion
and $asker
are automatically queried from the database.
https://laravel.com/docs/8.x/routing#route-model-binding
Afterwards you can access the question id normally and you have the notifiable user model.
public function postAnswer(Request $request, Question $question, User $asker) {
$validate_data = $request->validate([
'answer' => 'required',
]);
$answer = Answer::create([
'answer' => $request->answer,
'user_id' => auth()->user()->id,
'question_id' => $question->id,
]);
$asker = User::find($asker);
$asker->notify(new RepliedToThread($question));
return back();
}
Source