Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Global Scope Auth

I've created an anonimus global scope in the users model as below in order to get only public users in the frontend:

protected static function boot()
{
    parent::boot();

    static::addGlobalScope('is_public', function(Builder $builder) {
        $builder->where('is_public', '=', 1);
    });
}

But... when i need to perform the login in the backend i need of course to check for not-public users so i need to exclude the global scope.

Is this possibile using the default AuthController of laravel?

Many Thanks!!

like image 327
manuelprojects Avatar asked Sep 25 '22 17:09

manuelprojects


1 Answers

You just need to create two Models - one without global scope (i.e. AuthUser) and another with the global scope that extends the first one (i.e. User).

Then you can use AuthUser for authentication And User everywhere else.

like image 131
Anže Časar Avatar answered Oct 12 '22 14:10

Anže Časar