【发布时间】:2018-06-16 05:29:14
【问题描述】:
我有两个模型,用户和事件。我用status 列在用户和事件之间制作了一个数据透视表invitations。在我的事件模型定义中,我写了这个:
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;
}
但是当我在我的控制器中时:
$event = Event::where('id', $id)->with(['owner', 'participants'])->first();
我有以下错误:
(1/1) BadMethodCallException
Method addEagerConstraints does not exist.
有人知道为什么吗?
【问题讨论】: