Laravel的附加属性在通过依赖注入初始化模型时返回null。

huangapple go评论124阅读模式
英文:

Laravel appended attribute returns null when model is initiated through dependecy injection

问题

当我通过Laravel的路由依赖注入实例化模型对象时,我有一个附加的属性,它在执行Model::first()->appended_attribute时有效,但是当我像下面这样获取模型对象时:

public function update(UserRequest $request, User $user)
{
 $user->appended_attribute; // 返回 null       
}

当我访问它时,它返回null,尽管当我执行$user->toArray()时,附加属性确实存在,但值为null

如果我这样做:

public function update(UserRequest $request, User $user)
{
 User::find($user->id)->appended_attribute; // 返回正确的值
}

它可以正常工作。

附加属性的方法:

public function getCurrentRoleAttribute()
{
  return $this->roles->first();
}

我是否遗漏了什么?

英文:

I have an appended attribute that works when I do Model::first()->appended_attribute, but when get the model object instantiated through laravel's route dependency injection like below :

public function update(UserRequest $request, User $user)
{
 $user->appended_attribute; // returns null       

}

When I access it it returns null, Though when I do $user->toArray() the appended attributes does exist but with a value of null.
And if I do this :

public function update(UserRequest $request, User $user)
{
 User::find($user->id)->appended_attribute; // returns it's correct value

}

It does work.

The append attribute method :

public function getCurrentRoleAttribute()
{
  return $this->roles->first();
}

Is there anything I'm missing ?

答案1

得分: 0

这取决于一个可能尚未加载的关系?尝试将这段代码添加到你的访问器中。

public function getCurrentRoleAttribute()
{
    $this->loadMissing('roles');

    return $this->roles->first();
}
英文:

It depends on a relationship that might not be loaded? Try adding this to your accessor.

public function getCurrentRoleAttribute()
{
    $this->loadMissing('roles');

    return $this->roles->first();
}

答案2

得分: 0

Short answer:
这是对Spatie的权限包的错误使用。

Long answer:
感谢@IGP的答案,给了我一个有关缺失关系加载的提示,我发现不是关系缺失,而恰恰相反!!。
删除用户模型中的这行后,它正常工作:

protected $with = ['roles'];

这是因为我正在使用Spatie的权限包,它使用用户登录后提供的team_id来加载用户的roles关系(在该团队中)。现在,我将关系设置为使用$with属性预加载,实际上是在设置team_id之前加载关系!!,这就是为什么它返回null的原因。

对于没有提供更多关于问题的信息,我感到抱歉 (:'.

英文:

Short answer:

It's a miss use of spatie's permissions package.

Long answer:

Thanks to @IGP 's answer for giving me a hint about the missing relation load, I found out that it's not that it's missing, it's actually the opposite !!.
After removing this line from the user model it worked :

protected $with = ['roles'];

It was because I'm using spatie's permissions package which uses the team_id provided after the user's login to load the user's roles relation (in that team). Now me setting the relation to be eagerly loaded with the $with property is basically saying load the relation before setting the team_id !!, That's why it was returning null.

SORRY FOR NOT PROVIDING MORE INFO ABOUT THE ISSUE (:'

huangapple
  • 本文由 发表于 2023年3月7日 20:39:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75662094.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定