Skip to content

Commit 1fe2ef8

Browse files
KKSzymanowskitaylorotwell
authored andcommitted
[5.4] Add ability to remove a global scope with another global scope (#19657)
* Fix #19282 * Fix import order * Update Builder.php
1 parent ff65fc9 commit 1fe2ef8

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

src/Illuminate/Database/Eloquent/Builder.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -887,7 +887,11 @@ public function applyScopes()
887887

888888
$builder = clone $this;
889889

890-
foreach ($this->scopes as $scope) {
890+
foreach ($this->scopes as $identifier => $scope) {
891+
if (! isset($builder->scopes[$identifier])) {
892+
continue;
893+
}
894+
891895
$builder->callScope(function (Builder $builder) use ($scope) {
892896
// If the scope is a Closure we will just go ahead and call the scope with the
893897
// builder instance. The "callScope" method will properly group the clauses

tests/Database/DatabaseEloquentIntegrationTest.php

+36
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
use Exception;
66
use PHPUnit\Framework\TestCase;
7+
use Illuminate\Database\Eloquent\SoftDeletes;
78
use Illuminate\Database\Capsule\Manager as DB;
89
use Illuminate\Database\Eloquent\Relations\Pivot;
10+
use Illuminate\Database\Eloquent\SoftDeletingScope;
911
use Illuminate\Database\Eloquent\Model as Eloquent;
1012
use Illuminate\Database\Eloquent\Relations\Relation;
1113
use Illuminate\Pagination\AbstractPaginator as Paginator;
@@ -90,6 +92,14 @@ protected function createSchema()
9092
$table->string('name');
9193
$table->timestamps();
9294
});
95+
96+
$this->schema($connection)->create('soft_deleted_users', function ($table) {
97+
$table->increments('id');
98+
$table->string('name')->nullable();
99+
$table->string('email');
100+
$table->timestamps();
101+
$table->softDeletes();
102+
});
93103
}
94104

95105
$this->schema($connection)->create('non_incrementing_users', function ($table) {
@@ -1012,6 +1022,14 @@ public function testModelIgnoredByGlobalScopeCanBeRefreshed()
10121022
$this->assertNotNull($user->fresh());
10131023
}
10141024

1025+
public function testGlobalScopeCanBeRemovedByOtherGlobalScope()
1026+
{
1027+
$user = EloquentTestUserWithGlobalScopeRemovingOtherScope::create(['id' => 1, 'email' => '[email protected]']);
1028+
$user->delete();
1029+
1030+
$this->assertNotNull(EloquentTestUserWithGlobalScopeRemovingOtherScope::find($user->id));
1031+
}
1032+
10151033
public function testForPageAfterIdCorrectlyPaginates()
10161034
{
10171035
EloquentTestUser::create(['id' => 1, 'email' => '[email protected]']);
@@ -1224,6 +1242,24 @@ public static function boot()
12241242
}
12251243
}
12261244

1245+
class EloquentTestUserWithGlobalScopeRemovingOtherScope extends Eloquent
1246+
{
1247+
use SoftDeletes;
1248+
1249+
protected $table = 'soft_deleted_users';
1250+
1251+
protected $guarded = [];
1252+
1253+
public static function boot()
1254+
{
1255+
static::addGlobalScope(function ($builder) {
1256+
$builder->withoutGlobalScope(SoftDeletingScope::class);
1257+
});
1258+
1259+
parent::boot();
1260+
}
1261+
}
1262+
12271263
class EloquentTestPost extends Eloquent
12281264
{
12291265
protected $table = 'posts';

0 commit comments

Comments
 (0)