mardi 5 juillet 2022

getting infinite loop - adding scope on user model with traits

after adding the following trait to the user model, I get a 500 error because of an infinite loop.

    trait Multitenantable
    {
        public static function bootMultitenantable()
        {
            static::addGlobalScope('tenant_id', function (Builder $builder) {
                $tenant_id = 1;
                if ( auth()->check() )
                {
                    $tenant_id = Auth::user()->tenant_id;
                }
    
                $builder->where('tenant_id', '=', $tenant_id);
            });
        }
    }

when I remove either the trait from the user model or the if containing the auth() part (lines 7,8,9,10) from this trait, the infinite loop resolves.

I'm not familiar with the magic behind laravel, could someone explain why this happens?

and how I could add global scope for multitenancy to the user model like the others?

I've followed this instruction to add multitenancy to my laraval project.



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/RulnV6T
via IFTTT

Aucun commentaire:

Enregistrer un commentaire