3 days ago

Global Scope on User model not working.

Posted 3 days ago by fylzero

Trying to implement multi-tenancy using a single db approach. My scope works on everything except when I try to put it on the User model my app blows up. Won't even load. What am I doing wrong here?



namespace App\Scopes;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;

class CompanyScope implements Scope
    public function apply(Builder $builder, Model $model)
        if (auth()->check()) {
            $builder->where('company_id', auth()->user()->company_id);

Code on the user model...

protected static function booted()
    static::addGlobalScope(new CompanyScope);

I'm just trying to display only users that belong to the current users company_id.

What I'm seeing after adding this...

Please sign in or create an account to participate in this conversation.