获取无限循环 - 在用户模型上添加特征的范围

发布于 2025-02-13 02:39:39 字数 824 浏览 3 评论 0原文

将以下特征添加到用户模型之后,由于无限循环,我会遇到5​​00个错误。

    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);
            });
        }
    }

当我从用户模型中删除特征或IF包含auth()零件(第7,8,9,10行)的IF时,无限环路就可以解决。

我不熟悉Laravel背后的魔术,有人可以解释为什么会发生这种情况吗?

以及如何像其他人一样,如何将全球多种范围添加到用户模型中?

我已经关注此指令以增加我的多义务Laraval项目。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一张白纸 2025-02-20 02:39:39

我通过创建特质并在用户模型上使用特质来解决此问题,

use Illuminate\Database\Eloquent\Builder;

trait FilterByClass
{
    protected static function booted()
    {
        if( request()->session()->exists('auth')){
        $user = auth()->user();
            self::addGlobalScope(function(Builder $builder)use($user){
                if($user->hasRole('Head Teacher')){
                    return;
                }
                $builder->where('branch_id',$user->class_id);
            });

        }
       
    }
}

我认为循环发生了,因为您试图从用户模型中进行身份验证,同时该模型正在寻找认证的用户,因此使用了僵局。
因此,通过检查request() - > session() - >是否存在('auth'')在应用范围之前存在,确保它检查auth() - &gt ; user()仅在存在时。

I resolve this by creating trait and used the trait on user model

use Illuminate\Database\Eloquent\Builder;

trait FilterByClass
{
    protected static function booted()
    {
        if( request()->session()->exists('auth')){
        $user = auth()->user();
            self::addGlobalScope(function(Builder $builder)use($user){
                if($user->hasRole('Head Teacher')){
                    return;
                }
                $builder->where('branch_id',$user->class_id);
            });

        }
       
    }
}

I figured the loop happens because you are trying to authenticate from the user model and at the same time the model is looking for an authenticated user hence use are deadlocked.
so by checking if an request()->session()->exist('auth') exist before applying the scope will make sure it check for an auth()->user() only if it exist.

可遇━不可求 2025-02-20 02:39:39

显然,当我将身份验证条件添加到此特征时,我无法将特征添加到用户模型中。
如果我这样做,当用户模型启动时,它必须检查auth,则需要启动用户模型。这导致无限循环。

Apparently when I add the auth condition to this trait, I cannot add the trait to user model.
If I do, when the user model is booting, It must check the Auth, wich requires booting the user model. And this cause the infinite loop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文