Posts Learn Components Snippets Categories Tags Tools About
/

Laravel Get Total Relationship Count

Laravel get model based on the total number of relationship

Created on Aug 02, 2021

106 views

To get the model based on the total number of relationships, you can use the "has()" method and provide the condition just like the "where()" clause.

The code example below will return "Post" model that has greater than 6 comments.
<?php

# Posts hasMany Comments
$posts = Post::has('comments', '>', 6)->get();

If you like our tutorial, do make sure to support us by being our Patreon or buy us some coffee ☕️

Load comments for Laravel Get Total Relationship Count

new

PostSrc Code Components

Collection of Tailwind CSS components for everyone to use. Browse all of the components that are right for your project.

View Components

Sponsors 👑

+ Add Yours
)