Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.2] Add relationship subquery count #13414

Merged
merged 4 commits into from
May 11, 2016
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
35 changes: 35 additions & 0 deletions src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -999,6 +999,41 @@ public function with($relations)
return $this;
}

/**
* Add subselect queries to count the relations.
*
* @param mixed $relations
* @return $this
*/
public function withCount($relations)
{
// If no columns are set, add the default * columns.
if (is_null($this->query->columns)) {
$this->query->select(['*']);
}

if (is_string($relations)) {
$relations = func_get_args();
}

$relations = $this->parseWithRelations($relations);

foreach ($relations as $name => $constraints) {
// First determine the count query for the given relationship,
// then run the constraints callback to get the final query.
// This query will be added as subSelect query.
$relation = $this->getHasRelationQuery($name);
$query = $relation->getRelationCountQuery($relation->getRelated()->newQuery(), $this);

call_user_func($constraints, $query);

$asColumn = snake_case($name).'_count';
$this->selectSub($query->getQuery(), $asColumn);
}

return $this;
}

/**
* Parse a list of relations into individuals.
*
Expand Down
31 changes: 31 additions & 0 deletions tests/Database/DatabaseEloquentBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,37 @@ public function testDeleteOverride()
$this->assertEquals(['foo' => $builder], $builder->delete());
}

public function testWithCount()
{
$model = new EloquentBuilderTestModelParentStub;

$builder = $model->withCount('foo');

$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());
}

public function testWithCountAndSelect()
{
$model = new EloquentBuilderTestModelParentStub;

$builder = $model->select('id')->withCount('foo');

$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());
}

public function testWithCountAndContraintsAndHaving()
{
$model = new EloquentBuilderTestModelParentStub;

$builder = $model->where('bar', 'baz');
$builder->withCount(['foo' => function ($q) {
$q->where('bam', '>', 'qux');
}])->having('fooCount', '>=', 1);

$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());
$this->assertEquals(['qux', 'baz', 1], $builder->getBindings());
}

public function testHasWithContraintsAndHavingInSubquery()
{
$model = new EloquentBuilderTestModelParentStub;
Expand Down