如何在范围内使用wherePivot方法?

时间:2016-01-11 15:17:14

标签: laravel eloquent pivot

我有两个带枢轴的模型。

Domain
{
    public function projects()
    {
        return $this->belongsToMany('Project')->withPivot('is_live');
    }
}

Project
{
    public function domains()
    {
        return $this->belongsToMany('Domain')->withPivot('is_live');
    }
}

然后我尝试创建范围“具有Projects和is_live = true的域”,如Laravel Scope by pivot data values

public function scopeHasLiveProjects($query)
{
    $pivotTable = $this->projects()->getTable();
    return $query->whereHas('projects', function ($q) use ($pivotTable) {
            $q->where($pivotTable . '.is_live', true);
    });
}

但我如何在范围内使用雄辩的方法 wherePivot ('is_live','=',true)?有可能吗?!

1 个答案:

答案 0 :(得分:1)

public function scopeHasLiveProjects($query)
{
    return $query->whereHas('projects', function ($q) {
            $q->where('is_live', true);
    });
}

应该做所需的

相关问题