我有这个laravel范围代码
public function apply(Builder $builder, Model $model)
{
$builder->whereDoesntHave('getPermissionData');
}
到目前为止一切正常,我需要的是保持良好状态 像这样
public function apply(Builder $builder, Model $model)
{
/*
if($builder->whereHas('getPermissionData'))
{
check the relation
and do some code here
}
*/
$builder->whereDoesntHave('getPermissionData');
}
是否可能
答案 0 :(得分:2)
您需要在or
中相互创建两个不同的条件。您还应该将此条件包装在where
子句中,以便下一个条件也不会在or
中
public function apply(Builder $builder, Model $model) {
$builder->where(function ($query) {
$query->whereDoesntHave('getPermissionData')
->orWhereHas('getPermissionData', function ($permissionQuery) {
// Apply your condition to the relation query
});
});
}