laravel范围与关系和条件

时间:2019-05-25 23:30:50

标签: php laravel scope

我有这个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');
}

是否可能

1 个答案:

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