Skip to content

Commit 9d5f0f1

Browse files
committed
Added check for scopes to avoid them being applied twice. Thanks @saernz!
1 parent 4389592 commit 9d5f0f1

File tree

4 files changed

+23
-19
lines changed

4 files changed

+23
-19
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

7+
## [0.8.7] - 2020-07-02
8+
### Added
9+
- check for scopes to avoid them being applied twice. Thanks @saernz!
10+
711
## [0.8.6] - 2020-05-12
812
### Changed
913
- implementation of `debug_backtrace()` to reduce memory consumption. Thanks @saernz!

src/Traits/Buildable.php

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -292,19 +292,4 @@ function () use ($arguments, $cacheKey, $method) {
292292
}
293293
);
294294
}
295-
296-
/**
297-
* Apply the scopes if they haven't already been applied, if they have
298-
* just return the builder. This prevents scopes from being applied twice.
299-
*
300-
* @return static
301-
*/
302-
public function applyScopes()
303-
{
304-
if ($this->scopesAreApplied) {
305-
return $this;
306-
}
307-
308-
return parent::applyScopes();
309-
}
310295
}

src/Traits/Caching.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ public function __call($method, $parameters)
3232
return $result;
3333
}
3434

35+
public function applyScopes() : self
36+
{
37+
if ($this->scopesAreApplied) {
38+
return $this;
39+
}
40+
41+
return parent::applyScopes();
42+
}
43+
3544
protected function applyScopesToInstance()
3645
{
3746
if (! property_exists($this, "scopes")

tests/Integration/CachedBuilder/ScopeTest.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,14 +178,20 @@ public function testLocalScopesInRelationship()
178178

179179
public function testScopeNotAppliedTwice()
180180
{
181-
factory(Author::class, 10)->create();
182-
$user = factory(User::class)->create(["name" => "Anton Junior"]);
181+
$user = factory(User::class)
182+
->create(["name" => "Anton Junior"]);
183183
$this->actingAs($user);
184184
DB::enableQueryLog();
185-
$authorsA = (new AuthorBeginsWithScoped)
185+
186+
(new AuthorBeginsWithScoped)
186187
->get();
187188
$queryLog = DB::getQueryLog();
189+
188190
$this->assertCount(1, $queryLog);
189-
$this->assertCount(1, $queryLog[0]['bindings'], "There should only be 1 binding, scope is being applied more than once.");
191+
$this->assertCount(
192+
1,
193+
$queryLog[0]['bindings'],
194+
"There should only be 1 binding, scope is being applied more than once."
195+
);
190196
}
191197
}

0 commit comments

Comments
 (0)