Skip to content

Commit 71a4bb1

Browse files
committed
Merge branch 'note-on-with-count-method' of https://github.com/driesvints/docs into driesvints-note-on-with-count-method
2 parents a62f522 + dec5983 commit 71a4bb1

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

eloquent-relationships.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,14 @@ You may also alias the relationship count result, allowing multiple counts on th
845845

846846
echo $posts[0]->pending_comments_count;
847847

848+
When combining `withCount` with a `select` statement it's important to use `withCount` after the `select` call:
849+
850+
$query = App\Post::select(['title', 'body'])->withCount('comments');
851+
852+
echo $posts[0]->title;
853+
echo $posts[0]->body;
854+
echo $posts[0]->comments_count;
855+
848856
<a name="eager-loading"></a>
849857
## Eager Loading
850858

0 commit comments

Comments
 (0)