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

Created at 15-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

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Buy Me A Coffee

Don't forget to share this article! Help us spread the word by clicking the share button below.

We appreciate your support and are committed to providing you valuable and informative content.

We are thankful for your never ending support.