Laravel雄辩的渴望加载多态性关系
我使用Rappasoft/laravel-authentication-log
与我的用户
模型。它暴露了以下多态关系与用户模型:
public function authentications()
{
return $this->morphMany(AuthenticationLog::class, 'authenticatable')->latest('login_at');
}
现在,如果我运行$ user-> authentications-> some_property
($ user
是user:用户: :get()
集合),发出n+1个查询。我如何渴望加载这种关系?
我已经尝试了用户:: with('authentications') - > get()
,但似乎无法正常工作。我仍然得到n+1个查询。
I am using rappasoft/laravel-authentication-log
with my User
model. It exposes the following polymorphic relation to the User model:
public function authentications()
{
return $this->morphMany(AuthenticationLog::class, 'authenticatable')->latest('login_at');
}
Now, if I run $user->authentications->some_property
($user
is from User::get()
collection), it emits N+1 queries. How do I eager load this relationship?
I've tried User::with('authentications')->get()
but it doesn't seem to be working. I'm still getting N+1 queries.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,看来急切的负载正在正常工作。我正在使用
$ user-> getlastLoginat()
方法,该方法明确加载了关系:我已经将代码更改为
optional($ this-&this-> authertications-> first> first() ) - > login_at
,我不再generatig n+1查询Ok, It seems the eager loading is working as-is. I was using the
$user->getLastLoginAt()
method which loads the relationship explicitly:I've changed the code to
optional($this->authentications->first())->login_at
and I'm no longer generatig N+1 queries