How to Create a Truly Global Scope

Asked

Viewed 64 times

0

It is possible to create global scopes for Models, but even if it is Global, it is still necessary to specify within your Model that it will have this implementation.

I want to create a global scope that applies to all models.

Ref: https://laravel.com/docs/5.6/eloquent#query-Scopes

  • So, the feature is how it has to be done, the maximum is to make an inheritance of the Model class and use this inheritance with a Specified Global Scope, there is no other way.! that’s it?

1 answer

0

You could put for class Model inherit Scope, but that would entail changing something on the basis of Framework, which could entail future updates.

Then I recommend the following:

Create a class to apply the scope and extend to Model

abstract class BaseModel extends Model
{
   public static function boot()
   {
      parent::boot();

      static::addGlobalScope(new BaseScope);
   }
}

Then have your models extend from class BaseModel.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.