Posts Learn Components Snippets Categories Tags About
/

Laravel Get Total Relationship Count

Laravel get model based on the total number of relationship

Created on Aug 02, 2021

11 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();

Snippets by

PostSrc poster

PostSrc

Full stack developer mainly focusing on the front end side of the web. The main author of PostSrc and loves writing tutorial and guides online.

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's right for your project.

View Components

Other Code Snippets

More