Laravel Global Scope Auth

≯℡__Kan透↙ 提交于 2019-12-06 06:20:29

问题


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!!


回答1:


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.




回答2:


You can remove any global scope on the fly with the following method:

User::withoutGlobalScope('is_public')->get();



来源:https://stackoverflow.com/questions/34696134/laravel-global-scope-auth

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!