Help me little please. I can’t filtering single record. May be or not.
// In Controller
public function show(Post $post)
{
$post = Post::with(['comments' => function($query) {
$query->where('status', 'active');
}
])->find($post);
// can't filter comments
return view('post.show', ['post' => $post]);
}
Sort comments by active
2
Answers
Read docs https://laravel.com/docs/10.x/eloquent-relationships!
First of all, you have
Post
type filtered as$post
. Just add other thing dynamically to that and finally fetch the result. You don’t have to find it again!If your function in controller just accepts ID as argument, you simply can change your code to:
Use whereHas(),