Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.4] Add ability to remove a global scope with another global scope #19657

Merged
merged 3 commits into from
Jun 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 36 additions & 0 deletions tests/Database/DatabaseEloquentIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -1012,6 +1022,14 @@ public function testModelIgnoredByGlobalScopeCanBeRefreshed()
$this->assertNotNull($user->fresh());
}

public function testGlobalScopeCanBeRemovedByOtherGlobalScope()
{
$user = EloquentTestUserWithGlobalScopeRemovingOtherScope::create(['id' => 1, 'email' => '[email protected]']);
$user->delete();

$this->assertNotNull(EloquentTestUserWithGlobalScopeRemovingOtherScope::find($user->id));
}

public function testForPageAfterIdCorrectlyPaginates()
{
EloquentTestUser::create(['id' => 1, 'email' => '[email protected]']);
Expand Down Expand Up @@ -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';
Expand Down