方法addEagerConstraints不存在 [英] Method addEagerConstraints does not exist

查看:229
本文介绍了方法addEagerConstraints不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个模型,用户模型和事件模型.我用status列在用户和事件之间创建了一个数据透视表invitations.在我的事件模型定义中,我这样写:

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.

有人知道为什么吗?

推荐答案

当您尝试执行此操作时:

When you're trying to do this:

->with('participants')

Laravel希望获得一个关系实例.换句话说,您不能将这种方法用作关系.

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

这篇关于方法addEagerConstraints不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆