diff --git a/src/Illuminate/Database/Eloquent/Builder.php b/src/Illuminate/Database/Eloquent/Builder.php index 67641a8e11cc..dca6507d613b 100755 --- a/src/Illuminate/Database/Eloquent/Builder.php +++ b/src/Illuminate/Database/Eloquent/Builder.php @@ -887,7 +887,11 @@ public function applyScopes() $builder = clone $this; - foreach ($this->scopes as $scope) { + foreach ($this->scopes as $identifier => $scope) { + if (! isset($builder->scopes[$identifier])) { + continue; + } + $builder->callScope(function (Builder $builder) use ($scope) { // If the scope is a Closure we will just go ahead and call the scope with the // builder instance. The "callScope" method will properly group the clauses diff --git a/tests/Database/DatabaseEloquentIntegrationTest.php b/tests/Database/DatabaseEloquentIntegrationTest.php index c72960e4ba27..e632acd32192 100644 --- a/tests/Database/DatabaseEloquentIntegrationTest.php +++ b/tests/Database/DatabaseEloquentIntegrationTest.php @@ -4,8 +4,10 @@ use Exception; use PHPUnit\Framework\TestCase; +use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Capsule\Manager as DB; use Illuminate\Database\Eloquent\Relations\Pivot; +use Illuminate\Database\Eloquent\SoftDeletingScope; use Illuminate\Database\Eloquent\Model as Eloquent; use Illuminate\Database\Eloquent\Relations\Relation; use Illuminate\Pagination\AbstractPaginator as Paginator; @@ -90,6 +92,14 @@ protected function createSchema() $table->string('name'); $table->timestamps(); }); + + $this->schema($connection)->create('soft_deleted_users', function ($table) { + $table->increments('id'); + $table->string('name')->nullable(); + $table->string('email'); + $table->timestamps(); + $table->softDeletes(); + }); } $this->schema($connection)->create('non_incrementing_users', function ($table) { @@ -1012,6 +1022,14 @@ public function testModelIgnoredByGlobalScopeCanBeRefreshed() $this->assertNotNull($user->fresh()); } + public function testGlobalScopeCanBeRemovedByOtherGlobalScope() + { + $user = EloquentTestUserWithGlobalScopeRemovingOtherScope::create(['id' => 1, 'email' => 'taylorotwell@gmail.com']); + $user->delete(); + + $this->assertNotNull(EloquentTestUserWithGlobalScopeRemovingOtherScope::find($user->id)); + } + public function testForPageAfterIdCorrectlyPaginates() { EloquentTestUser::create(['id' => 1, 'email' => 'taylorotwell@gmail.com']); @@ -1224,6 +1242,24 @@ public static function boot() } } +class EloquentTestUserWithGlobalScopeRemovingOtherScope extends Eloquent +{ + use SoftDeletes; + + protected $table = 'soft_deleted_users'; + + protected $guarded = []; + + public static function boot() + { + static::addGlobalScope(function ($builder) { + $builder->withoutGlobalScope(SoftDeletingScope::class); + }); + + parent::boot(); + } +} + class EloquentTestPost extends Eloquent { protected $table = 'posts';