方法 addEagerConstraints 不存在 [英] Method addEagerConstraints does not exist
本文介绍了方法 addEagerConstraints 不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个模型,用户和事件.我在用户和事件之间制作了一个数据透视表,invitations
和 status
列.在我的事件模型定义中,我写了这个:
I have two models, User and Event. I made a pivot table, invitations
between User and Event with a status
column. In my Event model definition, I wrote this :
public function invited()
{
return $this->belongsToMany(User::class, 'invitations', 'event_id', 'user_id')
->withTimestamps()
->withPivot('status')
->orderByDesc('invitations.updated_at');
}
public function participants()
{
$event = $this->with([
'invited' => function ($query) {
$query->where('invitations.status', InvitationStatus::ACCEPTED)->get();
}
])->first();
return $event->invited;
}
但是当我在控制器中时:
But when in my controller I do :
$event = Event::where('id', $id)->with(['owner', 'participants'])->first();
我有以下错误:
(1/1) BadMethodCallException
Method addEagerConstraints does not exist.
有人知道为什么吗?
推荐答案
当您尝试这样做时:
->with('participants')
Laravel 期望得到一个关系实例.换句话说,您不能将此方法用作关系.
Laravel expects to get a relationship instance. In other words, you can't use this method as a relation.
这篇关于方法 addEagerConstraints 不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文