From a1c75cd4b0102083e487baa19ea73c1f5bd2d4b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=2E=20Nagy=20Gerg=C5=91?= Date: Wed, 13 Apr 2022 09:45:41 +0200 Subject: [PATCH] [9.x] Add withoutEagerLoads() method to Builder --- src/Illuminate/Database/Eloquent/Builder.php | 10 ++++++++++ tests/Database/DatabaseEloquentBuilderTest.php | 13 +++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/Illuminate/Database/Eloquent/Builder.php b/src/Illuminate/Database/Eloquent/Builder.php index 365247f69f68..1b41b2162d55 100755 --- a/src/Illuminate/Database/Eloquent/Builder.php +++ b/src/Illuminate/Database/Eloquent/Builder.php @@ -1546,6 +1546,16 @@ public function setEagerLoads(array $eagerLoad) return $this; } + /** + * Flush the relationships being eagerly loaded. + * + * @return $this + */ + public function withoutEagerLoads() + { + return $this->setEagerLoads([]); + } + /** * Get the default key name of the table. * diff --git a/tests/Database/DatabaseEloquentBuilderTest.php b/tests/Database/DatabaseEloquentBuilderTest.php index b8a0c54f0b17..044167daf033 100755 --- a/tests/Database/DatabaseEloquentBuilderTest.php +++ b/tests/Database/DatabaseEloquentBuilderTest.php @@ -666,6 +666,19 @@ public function testEagerLoadRelationsLoadTopLevelRelationships() $this->assertEquals(['foo'], $results); } + public function testEagerLoadRelationsCanBeFlushed() + { + $builder = m::mock(Builder::class.'[eagerLoadRelation]', [$this->getMockQueryBuilder()]); + + $builder->setEagerLoads(['foo']); + + $this->assertSame(['foo'], $builder->getEagerLoads()); + + $builder->withoutEagerLoads(); + + $this->assertEmpty($builder->getEagerLoads()); + } + public function testRelationshipEagerLoadProcess() { $builder = m::mock(Builder::class.'[getRelation]', [$this->getMockQueryBuilder()]);