简体   繁体   English

使用 Laravel 进行复杂的 eloquent 嵌套关系查询

[英]Complex eloquent nested relationship queries with Laravel

So I am building out a query with eloquent in my laravel app.所以我在我的 laravel 应用程序中建立了一个雄辩的查询。 The query has many different relationships being brought into it and is set up like this below:该查询有许多不同的关系被引入其中,并设置如下:

CourseEnrollment::whereIn('domain_id', $domain_ids)
                        ->whereHas('member', function($query)use ($filterUser){
                            if($filterUser === 'true'){
                                $query->where('email', 'NOT LIKE', '%@gmail.com%');
                                $query->where('email', 'NOT LIKE', '%@yahoo.com%');
                                $query->where('email', 'NOT LIKE', '%@boss.com%');
                                $query->where('email', 'NOT LIKE', '%@test.com%');
                            }
                            $query->where('is_mentor', 1)->orWhere(function ($q){
                                $q->where('privileges', '!=', 0);
                            });
                        })   
                        ->get();

If you look on the line that has the code $query->where(is_mentor, 1)->orWhere... you can see my attempt to try and create an orWhere situation that is using keys that are nested at two different points.如果您查看代码 $query->where(is_mentor, 1)->orWhere... 的行,您可以看到我尝试创建一个使用嵌套在两个不同点的键的 orWhere 情况。 Privileges is acutally not on the member relationship but is rather a key on the main CourseEnrollment table that I am querying.权限实际上不是在成员关系上,而是在我正在查询的主 CourseEnrollment 表上的一个键。 I need to be able to say if the enrollment.privileges are not equal to 0 or if the member.is_mentor key is equal to 1 then grab that enrollment, else don't grab that enrollment.我需要能够说如果enrollment.privileges 不等于0 或者如果member.is_mentor 键等于1 然后获取该注册,否则不要获取该注册。 As I have it right now I thought it was working but the issue I'm having is that when I combine this with the logic above that is filtering out emails, the orWhere is overriding that logic.正如我现在拥有的那样,我认为它正在工作,但我遇到的问题是,当我将它与上面过滤掉电子邮件的逻辑结合起来时,orWhere 会覆盖该逻辑。 For instance, I was filtering out all emails and this route should have been returning nothing but an enrollment was able to come through that had met both conditions of the orWhere statement.例如,我正在过滤掉所有的电子邮件,这条路线本应该什么都不返回,但是能够通过满足 orWhere 语句的两个条件的注册。 Is there a better way to compare differently nested keys in laravel?有没有更好的方法来比较 laravel 中不同的嵌套键?

I suggest group with where the query before use orWhere, for prevent not filter the previous wheres, is like add parenthesis enclosing the inner conditions.我建议在使用 orWhere 之前将查询与 where 分组,以防止不过滤以前的 where,就像添加括号括住内部条件一样。

CourseEnrollment::whereIn('domain_id', $domain_ids)
    ->whereHas('member', function ($query) use ($filterUser) {
        if ($filterUser === 'true') {
            $query->where('email', 'NOT LIKE', '%@gmail.com%');
            $query->where('email', 'NOT LIKE', '%@yahoo.com%');
            $query->where('email', 'NOT LIKE', '%@boss.com%');
            $query->where('email', 'NOT LIKE', '%@test.com%');
        }

        $query
            ->where(function ($sub) {
                $sub->where('is_mentor', 1);
                $sub->orWhere('privileges', '!=', 0);
            });
    })
    ->get();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM