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);
});
}
}