Skip to content

Commit c9cb36a

Browse files
committed
Merge remote-tracking branch 'upstream/8.x' into 8.x
2 parents add0e98 + acebcfc commit c9cb36a

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/Illuminate/Database/Eloquent/Builder.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,6 +1236,19 @@ public function without($relations)
12361236
return $this;
12371237
}
12381238

1239+
/**
1240+
* Set the relationships that should be eager loaded while removing any previously added eager loading specifications.
1241+
*
1242+
* @param mixed $relations
1243+
* @return $this
1244+
*/
1245+
public function withOnly($relations)
1246+
{
1247+
$this->eagerLoad = [];
1248+
1249+
return $this->with($relations);
1250+
}
1251+
12391252
/**
12401253
* Create a new instance of the model being queried.
12411254
*

tests/Database/DatabaseEloquentModelTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,15 @@ public function testWithoutMethodRemovesEagerLoadedRelationshipCorrectly()
327327
$this->assertEmpty($instance->getEagerLoads());
328328
}
329329

330+
public function testWithOnlyMethodLoadsRelationshipCorrectly()
331+
{
332+
$model = new EloquentModelWithoutRelationStub();
333+
$this->addMockConnection($model);
334+
$instance = $model->newInstance()->newQuery()->withOnly('taylor');
335+
$this->assertNotNull($instance->getEagerLoads()['taylor']);
336+
$this->assertArrayNotHasKey('foo', $instance->getEagerLoads());
337+
}
338+
330339
public function testEagerLoadingWithColumns()
331340
{
332341
$model = new EloquentModelWithoutRelationStub;

0 commit comments

Comments
 (0)