获取无限循环 - 在用户模型上添加特征的范围
将以下特征添加到用户模型之后,由于无限循环,我会遇到500个错误。
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我通过创建特质并在用户模型上使用特质来解决此问题,
我认为循环发生了,因为您试图从用户模型中进行身份验证,同时该模型正在寻找认证的用户,因此使用了僵局。
因此,通过检查
request() - > session() - >是否存在('auth'')
在应用范围之前存在,确保它检查auth() - &gt ; user()
仅在存在时。I resolve this by creating trait and used the trait on user model
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 anauth()->user()
only if it exist.显然,当我将身份验证条件添加到此特征时,我无法将特征添加到用户模型中。
如果我这样做,当用户模型启动时,它必须检查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.