Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ public function whereDoesntHave($relation, Closure $callback = null)
*/
public function withCount($relations)
{
if (empty($relations)) {
return $this;
}

if (is_null($this->query->columns)) {
$this->query->select([$this->query->from.'.*']);
}
Expand Down
9 changes: 8 additions & 1 deletion src/Illuminate/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ abstract class Model implements ArrayAccess, Arrayable, Jsonable, JsonSerializab
*/
protected $with = [];

/**
* The relations to eager load the count on every query.
*
* @var array
*/
protected $withCount = [];

/**
* The number of models to return for pagination.
*
Expand Down Expand Up @@ -809,7 +816,7 @@ public function newQueryWithoutScopes()
// Once we have the query builders, we will set the model instances so the
// builder can easily access any information it may need from the model
// while it is constructing and executing various queries against it.
return $builder->setModel($this)->with($this->with);
return $builder->setModel($this)->with($this->with)->withCount($this->withCount);
}

/**
Expand Down