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

[11.x] Merge in eager loads from nested where queries #54455

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
2 changes: 2 additions & 0 deletions src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,8 @@ public function where($column, $operator = null, $value = null, $boolean = 'and'
if ($column instanceof Closure && is_null($operator)) {
$column($query = $this->model->newQueryWithoutRelationships());

$this->eagerLoad = array_merge($this->eagerLoad, $query->getEagerLoads());

$this->query->addNestedWhereQuery($query->getQuery(), $boolean);
} else {
$this->query->where(...func_get_args());
Expand Down
3 changes: 3 additions & 0 deletions tests/Database/DatabaseEloquentBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1031,6 +1031,7 @@ public function testNestedWhere()
$nestedQuery = m::mock(Builder::class);
$nestedRawQuery = $this->getMockQueryBuilder();
$nestedQuery->shouldReceive('getQuery')->once()->andReturn($nestedRawQuery);
$nestedQuery->shouldReceive('getEagerLoads')->once()->andReturn([]);
$model = $this->getMockModel()->makePartial();
$model->shouldReceive('newQueryWithoutRelationships')->once()->andReturn($nestedQuery);
$builder = $this->getBuilder();
Expand Down Expand Up @@ -1092,6 +1093,7 @@ public function testWhereNot()
$nestedQuery = m::mock(Builder::class);
$nestedRawQuery = $this->getMockQueryBuilder();
$nestedQuery->shouldReceive('getQuery')->once()->andReturn($nestedRawQuery);
$nestedQuery->shouldReceive('getEagerLoads')->once()->andReturn([]);
$model = $this->getMockModel()->makePartial();
$model->shouldReceive('newQueryWithoutRelationships')->once()->andReturn($nestedQuery);
$builder = $this->getBuilder();
Expand Down Expand Up @@ -1120,6 +1122,7 @@ public function testOrWhereNot()
$nestedQuery = m::mock(Builder::class);
$nestedRawQuery = $this->getMockQueryBuilder();
$nestedQuery->shouldReceive('getQuery')->once()->andReturn($nestedRawQuery);
$nestedQuery->shouldReceive('getEagerLoads')->once()->andReturn([]);
$model = $this->getMockModel()->makePartial();
$model->shouldReceive('newQueryWithoutRelationships')->once()->andReturn($nestedQuery);
$builder = $this->getBuilder();
Expand Down
11 changes: 11 additions & 0 deletions tests/Database/DatabaseEloquentModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,17 @@ public function testWithWhereHasWithSpecificColumns()
$closure($builder);
}

public function testWithWhereHasWorksInNestedQuery()
{
$model = new EloquentModelWithWhereHasStub;
$instance = $model->newInstance()->newQuery()->where(fn (Builder $q) => $q->withWhereHas('foo:diaa,fares'));
$builder = m::mock(Builder::class);
$builder->shouldReceive('select')->once()->with(['diaa', 'fares']);
$this->assertNotNull($instance->getEagerLoads()['foo']);
$closure = $instance->getEagerLoads()['foo'];
$closure($builder);
}

public function testWithMethodCallsQueryBuilderCorrectlyWithArray()
{
$result = EloquentModelWithStub::with(['foo', 'bar']);
Expand Down