Skip to content

Commit 67b821d

Browse files
committed
Rename to withCount, auto column names, default select
1 parent 8d64b76 commit 67b821d

File tree

2 files changed

+52
-29
lines changed

2 files changed

+52
-29
lines changed

src/Illuminate/Database/Eloquent/Builder.php

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -762,27 +762,6 @@ public function orWhere($column, $operator = null, $value = null)
762762
return $this->where($column, $operator, $value, 'or');
763763
}
764764

765-
/**
766-
* Add a relationship count select to the query.
767-
*
768-
* @param string $relation
769-
* @param string $as
770-
* @param \Closure|null $callback
771-
* @return \Illuminate\Database\Eloquent\Builder|static
772-
*/
773-
public function selectCount($relation, $as, Closure $callback = null)
774-
{
775-
$relation = $this->getHasRelationQuery($relation);
776-
777-
$query = $relation->getRelationCountQuery($relation->getRelated()->newQuery(), $this);
778-
779-
if ($callback) {
780-
call_user_func($callback, $query);
781-
}
782-
783-
return $this->selectSub($query->getQuery(), $as);
784-
}
785-
786765
/**
787766
* Add a relationship count / exists condition to the query.
788767
*
@@ -1020,6 +999,41 @@ public function with($relations)
1020999
return $this;
10211000
}
10221001

1002+
/**
1003+
* Add subselect queries to count the relations.
1004+
*
1005+
* @param mixed $relations
1006+
* @return $this
1007+
*/
1008+
public function withCount($relations)
1009+
{
1010+
// If no columns are set, add the default * columns.
1011+
if (is_null($this->query->columns)) {
1012+
$this->query->select(['*']);
1013+
}
1014+
1015+
if (is_string($relations)) {
1016+
$relations = func_get_args();
1017+
}
1018+
1019+
$relations = $this->parseWithRelations($relations);
1020+
1021+
foreach ($relations as $name => $constraints) {
1022+
// First determine the count query for the given relationship,
1023+
// then run the constraints callback to get the final query.
1024+
// This query will be added as subSelect query.
1025+
$relation = $this->getHasRelationQuery($name);
1026+
$query = $relation->getRelationCountQuery($relation->getRelated()->newQuery(), $this);
1027+
1028+
call_user_func($constraints, $query);
1029+
1030+
$asColumn = snake_case($name).'_count';
1031+
$this->selectSub($query->getQuery(), $asColumn);
1032+
}
1033+
1034+
return $this;
1035+
}
1036+
10231037
/**
10241038
* Parse a list of relations into individuals.
10251039
*

tests/Database/DatabaseEloquentBuilderTest.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -455,25 +455,34 @@ public function testDeleteOverride()
455455
$this->assertEquals(['foo' => $builder], $builder->delete());
456456
}
457457

458-
public function testSelectCount()
458+
public function testWithCount()
459459
{
460460
$model = new EloquentBuilderTestModelParentStub;
461461

462-
$builder = $model->selectCount('foo', 'fooCount');
462+
$builder = $model->withCount('foo');
463463

464-
$this->assertEquals('select (select count(*) from "eloquent_builder_test_model_close_related_stubs" where "eloquent_builder_test_model_parent_stubs"."foo_id" = "eloquent_builder_test_model_close_related_stubs"."id") as "fooCount" from "eloquent_builder_test_model_parent_stubs"', $builder->toSql());
464+
$this->assertEquals('select *, (select count(*) from "eloquent_builder_test_model_close_related_stubs" where "eloquent_builder_test_model_parent_stubs"."foo_id" = "eloquent_builder_test_model_close_related_stubs"."id") as "foo_count" from "eloquent_builder_test_model_parent_stubs"', $builder->toSql());
465465
}
466466

467-
public function testSelectCountWithSelectAndContraintsAndHaving()
467+
public function testWithCountAndSelect()
468468
{
469469
$model = new EloquentBuilderTestModelParentStub;
470470

471-
$builder = $model->where('bar', 'baz')->select('*');
472-
$builder->selectCount('foo', 'fooCount', function ($q) {
471+
$builder = $model->select('id')->withCount('foo');
472+
473+
$this->assertEquals('select "id", (select count(*) from "eloquent_builder_test_model_close_related_stubs" where "eloquent_builder_test_model_parent_stubs"."foo_id" = "eloquent_builder_test_model_close_related_stubs"."id") as "foo_count" from "eloquent_builder_test_model_parent_stubs"', $builder->toSql());
474+
}
475+
476+
public function testWithCountAndContraintsAndHaving()
477+
{
478+
$model = new EloquentBuilderTestModelParentStub;
479+
480+
$builder = $model->where('bar', 'baz');
481+
$builder->withCount(['foo' => function ($q) {
473482
$q->where('bam', '>', 'qux');
474-
})->having('fooCount', '>=', 1);
483+
}])->having('fooCount', '>=', 1);
475484

476-
$this->assertEquals('select *, (select count(*) from "eloquent_builder_test_model_close_related_stubs" where "eloquent_builder_test_model_parent_stubs"."foo_id" = "eloquent_builder_test_model_close_related_stubs"."id" and "bam" > ?) as "fooCount" from "eloquent_builder_test_model_parent_stubs" where "bar" = ? having "fooCount" >= ?', $builder->toSql());
485+
$this->assertEquals('select *, (select count(*) from "eloquent_builder_test_model_close_related_stubs" where "eloquent_builder_test_model_parent_stubs"."foo_id" = "eloquent_builder_test_model_close_related_stubs"."id" and "bam" > ?) as "foo_count" from "eloquent_builder_test_model_parent_stubs" where "bar" = ? having "fooCount" >= ?', $builder->toSql());
477486
$this->assertEquals(['qux', 'baz', 1], $builder->getBindings());
478487
}
479488

0 commit comments

Comments
 (0)