Home / Snippets / Laravel Get Total Relationship Count
Laravel Get Total Relationship Count cover

Laravel Get Total Relationship Count

257

3 years ago

0 comments

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();
notion avatar

Alaz

Week-end developer currently experimenting with web, mobile, and all things programming.

Topics:

Frontend

Resource

Average

Average

Support Us

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

Welcome to PostSrc V3

PostSrc Dark Logo

You have to login to favorite this