Laravel Eloquent属于多对查询

时间:2019-01-09 00:25:13

标签: php laravel eloquent

我正在尝试与设置为belongsToMany的位置建立联系,

内容->许可证->组织

因此,在我的内容模型中说一个更简单的方法是,我具有以下两种关系:

public function licenses_public() {
    return $this->belongsToMany(License::class)
        ->whereDoesntHave('orgs');
}

public function licenses_private() {
    return $this->belongsToMany(License::class)
        ->whereHas('orgs', function($query) {
            $query->where('org_id', Auth::user()->org_id);
    });
}

现在,我希望将两者结合起来,并且基本上做到这一点:

public function getLicensesFilteredAttribute() {
    if ($this->licenses_private->count()) {
        return $this->licenses_private;
    }
    return $this->licenses;
}

如果任何许可证具有一个已登录用户org_id的组织,则仅返回那些,否则返回未绑定到任何组织的所有许可证。

在查询级别上是否有可能?作为一种关系?

0 个答案:

没有答案