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

Updated at 14-Sep-2021, By samar

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

In this tutorial, we will try to find the solution to "Get the post details if it has at least one comment in comments table" through programming.

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.
  • 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();
    

    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