Skip to content

Commit 1f4247b

Browse files
paulofreitastaylorotwell
authored andcommitted
Document Eloquent\Builder::withCount() aliasing support
Documents the feature added by laravel/framework#15279
1 parent b23f086 commit 1f4247b

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

eloquent-relationships.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,18 @@ You may add the "counts" for multiple relations as well as add constraints to th
675675
echo $posts[0]->votes_count;
676676
echo $posts[0]->comments_count;
677677

678+
If needed, you may also alias a count to something else, allowing multiple counts on the same relationship name:
679+
680+
$posts = Post::withCount([
681+
'comments',
682+
'comments AS pending_comments' => function ($query) {
683+
$query->where('approved', false);
684+
}
685+
])->get();
686+
687+
echo $posts[0]->comments_count;
688+
echo $posts[0]->pending_comments_count;
689+
678690
<a name="eager-loading"></a>
679691
## Eager Loading
680692

0 commit comments

Comments
 (0)