Laravel OrWhereNull 给出错误的结果

问题描述 投票:0回答:1

我有一个很长的查询生成器,我基本上在其中搜索表,然后根据传递的查询字符串过滤结果:

$projects = Listing::query()
            ->when(request('q'), function($builder) {
                $builder->searchQuery(request('q'));
            })
            ->when(request('tags'), function($builder) {
                $tags = request('tags');

                $builder->whereHas('tags', function($builder) use ($tags) {
                    $builder->whereIn('name', $tags);
                });
            })
            ->when(request('categories'), function($builder) {
                $categories = request('categories');

                $builder->whereHas('categories', function($builder) use ($categories) {
                    $builder->whereIn('name', $categories);
                });
            })
            ->when(request('countries'), function($builder) {
                $countries = request('countries');

                $builder->when(count($countries),function ($builder) use ($countries) {
                    $builder->whereHas('location', function($builder) use ($countries) {
                        $builder->where( function($builder) use ($countries) {
                            foreach ($countries as $country) {
                                $builder->orWhere('country', 'LIKE', '%' . $country . '%');
                                //$builder->orWhere('name', 'LIKE', '%' . $country . '%');
                            }
                        });
                    });
                }); 
            })
            ->when(request('opensource'), function($builder) {
                $builder->where('open_source', request('opensource'));
            })
            ->when(request('types'), function($builder) {
                $types = request('types');
                if (in_array("Other", $types)) {
                    $key = array_search("Other", $types);
                    $types[$key] = NULL;

                    $builder->whereIn('type', $types)->orWhereNull('type');
                } else {
                    $builder->whereIn('type', $types);   
                }
            })
            ->when(request('organizationtypes'), function($builder) {
                $organizationtypes = request('organizationtypes');
                if (in_array("Other", $organizationtypes)) {
                    $key = array_search("Other", $organizationtypes);
                    $organizationtypes[$key] = NULL;

                    $builder->whereIn('organization_type', $organizationtypes)->orWhereNull('organization_type');
                } else {
                    $builder->whereIn('organization_type', $organizationtypes);   
                }
            })
            ->when(request('status'), function($builder) {
                $status = request('status');
                if ($status == "Show active projects only") {
                    $builder->whereIn('status', ['Active', 'N/A']);
                } else {
                    $builder = $builder;
                }
            })
            ->orderBy('created', 'DESC')
            ->paginate(50);

查询运行良好,但是,我的问题在于这部分:

$status = request('status');
if ($status == "Show active projects only") {
     $builder->whereIn('status', ['Active', 'N/A']);
} else {
     $builder = $builder;
}

我希望如果 $status == “仅显示活动项目”,则显示状态为“活动”、“N/A”和 null 的项目。但是,当我将 if 部分修改为:

$builder->whereIn('status', ['Active', 'N/A'])->orWhereNull('status');

我得到了错误的结果。我做错了什么?

laravel eloquent
1个回答
0
投票

尝试将

whereIn()
orWhereNull()
括在另一个位置。 我怀疑
orWhereNull()
的扩展范围超出了状态范围内的限制。

->when(request('status'), function($builder) {
    $status = request('status');
    if ($status == "Show active projects only") {
        $builder->where(function($query) {
            return $query->whereIn('status', ['Active', 'N/A'])
                         ->orWhereNull('status');
        });
    } else {
        $builder = $builder;
    }
})
© www.soinside.com 2019 - 2024. All rights reserved.