Can not call Auth class inside Scope class in Laravel 5.2

I added External Scope for User Model. Simply created Scope, with the name DeveloperScope

use IlluminateDatabaseEloquentScope;
use IlluminateDatabaseEloquentBuilder;
use IlluminateDatabaseEloquentModel;
use Auth;

class DeveloperScope implements Scope
{
    public function apply(Builder $builder, Model $model)
    {
        if(Auth::user()->id != 1){
            $builder->where('id', '<>', 1);
        }
    }
}

Then, I called this scope for User model.

User Model

use IlluminateAuthAuthenticatable;
use IlluminateDatabaseEloquentModel;
use IlluminateAuthPasswordsCanResetPassword;
use IlluminateContractsAuthAuthenticatable as AuthenticatableContract;
use IlluminateContractsAuthCanResetPassword as CanResetPasswordContract;
use IlluminateDatabaseEloquentSoftDeletes;
use AppModelsScopesDeveloperScope;

class User extends Model implements AuthenticatableContract, CanResetPasswordContract
{
    use SoftDeletes;
    use Authenticatable, CanResetPassword;


    protected $table = 'users';

    protected $fillable = ['first_name', 'last_name', 'email', 'password'];

    protected $hidden = ['password', 'remember_token'];

    protected $dates = ['deleted_at'];

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

        static::addGlobalScope(new DeveloperScope);
    }
}

It works well, when I don’t use Auth::class inside DeveloperScope class. The reason is that, I just want to hide the main user for another users for all Eloquent methods. Of course, I can use session instead of Auth and retrieve user id. But it is still interesting for me, why browser gives an error: ERR_EMPTY_RESPONSE while using Auth::class ?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The method what you are looking for is exactly Auth::hasUser(). The method was added in Laravel 5.6 through my PR. (So you need to upgrade Laravel first)

[5.6] Add ability to determine if the current user is ALREADY authenticated without triggering side effects by mpyw · Pull Request #24518 · laravel/framework

use IlluminateDatabaseEloquentScope;
use IlluminateDatabaseEloquentBuilder;
use IlluminateDatabaseEloquentModel;
use IlluminateSupportFacadesAuth;

class DeveloperScope implements Scope
{
    public function apply(Builder $builder, Model $model)
    {
        if (!Auth::hasUser() || Auth::user()->id != 1) {
            $builder->where('id', '<>', 1);
        }
    }
}

Just call Auth::hasUser() to prevent Auth::user() from causing side effects.

Method 2

Try injecting the IlluminateAuthGuard class and then call the user method on it. e.g.:

$this->auth->user();

You should be fine with just type-hinting it since Laravel will automatically new up an instance for you.

For more info check out the API: https://laravel.com/api/5.2/Illuminate/Auth/Guard.html


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x