Skip to content

Commit 40b0217

Browse files
fernandobandeirataylorotwell
authored andcommitted
[5.3] Fix column overlaping on withCount (#16895)
* Fixes ambiguous column on withCount * Fix tests
1 parent 9f80a2a commit 40b0217

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

src/Illuminate/Database/Eloquent/Builder.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1078,7 +1078,7 @@ public function without($relations)
10781078
public function withCount($relations)
10791079
{
10801080
if (is_null($this->query->columns)) {
1081-
$this->query->select(['*']);
1081+
$this->query->select([$this->query->from.'.*']);
10821082
}
10831083

10841084
$relations = is_array($relations) ? $relations : func_get_args();

tests/Database/DatabaseEloquentBuilderTest.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ public function testWithCount()
587587

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

590-
$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());
590+
$this->assertEquals('select "eloquent_builder_test_model_parent_stubs".*, (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());
591591
}
592592

593593
public function testWithCountAndSelect()
@@ -620,7 +620,7 @@ public function testWithCountAndContraintsAndHaving()
620620
$q->where('bam', '>', 'qux');
621621
}])->having('foo_count', '>=', 1);
622622

623-
$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 "foo_count" >= ?', $builder->toSql());
623+
$this->assertEquals('select "eloquent_builder_test_model_parent_stubs".*, (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 "foo_count" >= ?', $builder->toSql());
624624
$this->assertEquals(['qux', 'baz', 1], $builder->getBindings());
625625
}
626626

@@ -630,7 +630,7 @@ public function testWithCountAndRename()
630630

631631
$builder = $model->withCount('foo as foo_bar');
632632

633-
$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_bar_count" from "eloquent_builder_test_model_parent_stubs"', $builder->toSql());
633+
$this->assertEquals('select "eloquent_builder_test_model_parent_stubs".*, (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_bar_count" from "eloquent_builder_test_model_parent_stubs"', $builder->toSql());
634634
}
635635

636636
public function testWithCountMultipleAndPartialRename()
@@ -639,7 +639,7 @@ public function testWithCountMultipleAndPartialRename()
639639

640640
$builder = $model->withCount(['foo as foo_bar', 'foo']);
641641

642-
$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_bar_count", (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());
642+
$this->assertEquals('select "eloquent_builder_test_model_parent_stubs".*, (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_bar_count", (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());
643643
}
644644

645645
public function testHasWithContraintsAndHavingInSubquery()

0 commit comments

Comments
 (0)