Get the post details if it has at least one comment in comments table

If you want to get the post details of a particular post which has at least one commet in comments table then you can use whereHas() method with callback function to get the post.

Answers 1
  • Get the post details if it has at least one comment in comments table

    $posts = Post::whereHas('comments', function($query){
                $query->where('post_id', 1);
            })->get();
    
    0

    It will return the post details if there are comments regarding this particular post (post_id is 1 in comments table) exists in the comments table.

  • Back to code snippet queries related laravel