如何禁用或设置为空数组模型属性$ with以在需要时不急于加载默认模型关系?

时间:2018-11-12 10:04:36

标签: laravel-5 eloquent

我有这样的东西:

class User
{
    protected $with = ['organisation'];
}

class Group
{
    public function members(): BelongsToMany
    {
        return $this->belongsToMany(User::class, 'groups_members', 'group_id', 'user_id', 'id', 'id')
                    ->select(['users.id', 'users.name', 'users.email']);
    }
}

如何防止组成员加载组织关系?

1 个答案:

答案 0 :(得分:1)

您可以使用without()

public function members(): BelongsToMany
{
    return $this->belongsToMany(User::class, 'groups_members', 'group_id', 'user_id', 'id', 'id')
                ->select(['users.id', 'users.name', 'users.email'])
                ->without('organisation');
}