Method addEagerConstraints does not exist

不问归期 提交于 2020-01-02 04:47:12

问题


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();

I have the following error :

(1/1) BadMethodCallException
Method addEagerConstraints does not exist.

Does someone know why?


回答1:


When you're trying to do this:

->with('participants')

Laravel expects to get a relationship instance. In other words, you can't use this method as a relation.




回答2:


where should come before with. This is what eloquent expects.

It should be like this:

$event = Event::with(['owner', 'participants'])->where('id', $id)->first();


来源:https://stackoverflow.com/questions/48129911/method-addeagerconstraints-does-not-exist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!