Reputation: 185
When I use findOrFail in my code it doesn't work
I've tried the find method and it works as I expected
public function show(Question $question, Reply $reply) {
return $question->replies->findOrFail($reply->id);
}
BadMethodCallException: Method Illuminate\Database\Eloquent\Collection::firstOrFail does not exist. in file
Upvotes: 2
Views: 3442
Reputation: 2602
Since replies
is a Question relationship, if you use $question->replies
, it returns a Illuminate\Database\Eloquent\Collection
instance, and the findOrFail
method is not available, because this methods belongs to the Illuminate\Database\Eloquent\Builder
class.
If you want to use findOrFail
to build your query, you must use it like this:
public function show(Question $question, Reply $reply) {
return $question->replies()->findOrFail($reply->id);
}
But, as mentioned in the comments by @porloscerrosΨ, you have Reply $reply
as a parameter in your function. You can simply return $reply:
public function show(Reply $reply) {
return $reply;
}
Hope it helps.
Upvotes: 5