Skip to content

Commit

Permalink
Document Eloquent\Builder::withCount() aliasing support
Browse files Browse the repository at this point in the history
Documents the feature added by laravel/framework#15279
  • Loading branch information
paulofreitas authored and taylorotwell committed Mar 13, 2017
1 parent b23f086 commit 1f4247b
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions eloquent-relationships.md
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,18 @@ You may add the "counts" for multiple relations as well as add constraints to th
echo $posts[0]->votes_count;
echo $posts[0]->comments_count;

If needed, you may also alias a count to something else, allowing multiple counts on the same relationship name:

$posts = Post::withCount([
'comments',
'comments AS pending_comments' => function ($query) {
$query->where('approved', false);
}
])->get();

echo $posts[0]->comments_count;
echo $posts[0]->pending_comments_count;

<a name="eager-loading"></a>
## Eager Loading

Expand Down

0 comments on commit 1f4247b

Please sign in to comment.