A
A
Arman2020-01-25 12:40:55
Laravel
Arman, 2020-01-25 12:40:55

How to disable global scope for one controller?

I implemented a scope as a separate class and connected it to the boot model https://laravel.com/docs/6.x/eloquent#global-scopes
everything works fine throughout the project, now I need to disable this scope for one controller so that it is not used in :

public function update(Post $post, Request $request) {}
public function delete(Post $post) {}

those. this scope is also used in dependency injection, and it is difficult to add conditions there.
So far, from ideas: in the boot controller, run some static method of the desired model and pass that you do not need to use the scope.
/**
 * @inheritDoc
 */
protected static function boot(): void
{
    parent::boot();

    in_array(VisibleScope::class, self::$disabledGlobalScopes) || static::addGlobalScope(new VisibleScope());
}

/**
 * @var array
 */
protected static $disabledGlobalScopes = [];

/**
 * @param string $scope
 */
public static function disableGlobalScope(string $scope): void
{
    static::$disabledGlobalScopes[] = $scope;
}

Maybe there is an easier option?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
NubasLol, 2020-01-27
@NubasLol

https://laravel.com/docs/6.x/routing#route-model-b...
There are options.

public function boot()
{
    parent::boot();

    Route::bind('page-no-scope', function ($value) {
        return App\Page::withoutGlobalScope()->firstOrFail();
    });
}

J
JhaoDa, 2020-01-25
@JhaoDa

Maybe there is an easier option?
Yes, it is described there in the documentation.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question