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
14 changes: 11 additions & 3 deletions src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -381,9 +381,17 @@ public function withAggregate($relations, $column, $function = null)

$relation = $this->getRelationWithoutConstraints($name);

$expression = $function
? sprintf('%s(%s)', $function, $this->getQuery()->getGrammar()->wrap($column))
: $column;
if ($function) {
$expression = sprintf(
'%s(%s)',
$function,
$this->getQuery()->getGrammar()->wrap(
$column === '*' ? $column : $relation->getRelated()->qualifyColumn($column)
)
);
} else {
$expression = $column;
}

// Here, we will grab the relationship sub-query and prepare to add it to the main query
// as a sub-select. First, we'll get the "has" query and use that to get the relation
Expand Down
28 changes: 28 additions & 0 deletions tests/Database/DatabaseEloquentBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,24 @@ public function testWithCountAndGlobalScope()
$this->assertSame('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" limit 1) as "foo_count" from "eloquent_builder_test_model_parent_stubs"', $builder->toSql());
}

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

$builder = $model->withMin('foo', 'price');

$this->assertSame('select "eloquent_builder_test_model_parent_stubs".*, (select min("eloquent_builder_test_model_close_related_stubs"."price") 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" limit 1) as "foo_min_price" from "eloquent_builder_test_model_parent_stubs"', $builder->toSql());
}

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

$builder = $model->withMin('roles', 'id');

$this->assertSame('select "eloquent_builder_test_model_parent_stubs".*, (select min("eloquent_builder_test_model_far_related_stubs"."id") from "eloquent_builder_test_model_far_related_stubs" inner join "user_role" on "eloquent_builder_test_model_far_related_stubs"."id" = "user_role"."related_id" where "eloquent_builder_test_model_parent_stubs"."id" = "user_role"."self_id" limit 1) as "roles_min_id" from "eloquent_builder_test_model_parent_stubs"', $builder->toSql());
}

public function testWithCountAndConstraintsAndHaving()
{
$model = new EloquentBuilderTestModelParentStub;
Expand Down Expand Up @@ -1482,6 +1500,16 @@ public function activeFoo()
{
return $this->belongsTo(EloquentBuilderTestModelCloseRelatedStub::class, 'foo_id')->where('active', true);
}

public function roles()
{
return $this->belongsToMany(
EloquentBuilderTestModelFarRelatedStub::class,
'user_role',
'self_id',
'related_id'
);
}
}

class EloquentBuilderTestModelCloseRelatedStub extends Model
Expand Down