Laravel 5 向whereHas添加额外查询

Laravel 5 向whereHas添加额外查询,laravel-5,laravel-nova,Laravel 5,Laravel Nova,我正在选择与经过身份验证的用户属于同一组织的所有广播,这对->whereHas()非常有效,但是如果我想添加一个过滤器,只显示广播在哪里发布是真的,该怎么办 public static function indexQuery(NovaRequest $request, $query) { if (Auth::user()->isAdmin()) { return $query; }else{ return $query->whereHa

我正在选择与经过身份验证的用户属于同一组织的所有广播,这对
->whereHas()
非常有效,但是如果我想添加一个过滤器,只显示
广播
在哪里发布
真的
,该怎么办

public static function indexQuery(NovaRequest $request, $query)
{
    if (Auth::user()->isAdmin()) {
        return $query;
    }else{
        return $query->whereHas('organizations', function($q){
            $q->where('organization_id', Auth::user()->organization_id);
        });
    }
}
模型

public function organizations()
{
    return $this->belongsToMany('App\Models\Organization');
}

public function broadcasts()
{
    return $this->belongsToMany('App\Models\Broadcast');
}

您可以向您的
广播
模型添加一个查询范围,该查询范围将仅查询
广播
,其中
已发布
真实
(这对于您需要发布广播的应用程序中的未来查询非常有用):

Broadcast.php(或模型文件)

然后在您的代码中,以及查询的
->published()
范围中:

public static function indexQuery(NovaRequest $request, $query)
{
    if (Auth::user()->isAdmin()) {
        return $query;
    } else {
        return $query->published()
            ->whereHas('organizations', function($q){
                $q->where('organization_id', Auth::user()->organization_id);
            });
    }
}

您可以向您的
广播
模型添加一个查询范围,该查询范围将仅查询
广播
,其中
已发布
真实
(这对于您需要发布广播的应用程序中的未来查询非常有用):

Broadcast.php(或模型文件)

然后在您的代码中,以及查询的
->published()
范围中:

public static function indexQuery(NovaRequest $request, $query)
{
    if (Auth::user()->isAdmin()) {
        return $query;
    } else {
        return $query->published()
            ->whereHas('organizations', function($q){
                $q->where('organization_id', Auth::user()->organization_id);
            });
    }
}