[英]Laravel Eloquent ORM: Building nested conditional queries
I have 5 user titles, defined by booleans: CEO, executive, manager, employee & intern. 我有5个由booleans定义的用户标题:CEO,执行官,经理,员工和实习生。
I'm building a user search API, and want to turn on/off eloquent queries, returning users with selected titles. 我正在构建一个用户搜索API,并希望打开/关闭雄辩的查询,以返回具有选定标题的用户。
So if I were to search for managers and employees, the query should be 因此,如果我要搜索经理和员工,则查询应为
$users = User::where(function($query)
{
$query->orWhere('manager')->orWhere('employee');
})
->where([ADDITIONAL CONSTRAINTS... (like age)])->get();
The furthest I have came is: 我最远的是:
$query = User::query();
//the respective titles are turned on by 1 and turned off by 0
if($CEO) {
$query = $query->orWhere('CEO');
}
if($executive) {
$query = $query->orWhere('executive');
}
//And so on for each title
In the end the additional where constraints get added like this: 最后,在其他地方添加了约束,如下所示:
$users = $query->where([Additional constraints])->get();
When searching for managers and employees, the final query would be: 在搜索经理和员工时,最终查询将是:
$users = User::orWhere('manager')->orWhere('employee')
->where([ADDITIONAL CONSTRAINTS... (like age)])->get();
The result of this query is that the additional constraints are not always met, because there are orwhere queries before, which allow for unwanted instances to get selected. 此查询的结果是,并非总是满足附加约束,因为之前存在orwhere查询,这些查询允许选择不需要的实例。
I tried replacing the orWhere's with where's, but then users need to check positive for each selected title to get selected. 我尝试用where's替换orWhere's,但是用户需要为每个选定的标题检查正数才能被选中。 So if I wanted to search for managers and employees, I might get none, because there isn't any user with both titles.
因此,如果我想搜索经理和员工,可能会一无所获,因为没有用户同时拥有这两个职位。
The goal: 目标:
Additional comments: 附加评论:
I know that I could also eliminate every other model instead of searching for wanted models. 我知道我也可以消除其他所有模型,而不必搜索所需模型。 If I would search for managers and employees, I could set where('CEO', '!=', 1) for each unwanted title.
如果我要搜索经理和员工,则可以为每个不需要的标题设置where('CEO','!=',1)。 I don't want this, because Users with two titles, like employee and interim would get excluded in some cases.
我不希望这样,因为在某些情况下,具有雇员和临时工两个头衔的用户会被排除在外。
I know that I could write nested conditional queries for each scenario ie (manager & ceo, interim & ceo & executive and on ...), but that would take 25 queries and simply is not easily scalable (exponential more queries) if additional user titles are added. 我知道我可以针对每种情况(例如,经理和首席执行官,临时和首席执行官和行政人员等)编写嵌套的条件查询,但是这将需要25个查询,并且如果有额外的用户,则不容易扩展(指数级查询)标题已添加。
It has to be an Eloquent solution. 它必须是一种雄辩的解决方案。
Users can have multiple titles. 用户可以有多个标题。
I have thought hard about this problem, thanks! 我已经为这个问题苦苦思索,谢谢!
Maybe you can do something like this ? 也许您可以做这样的事情? (with use function keyword)
(带有use function关键字)
<?php
$filters = ['manager', 'employee'];
$users = User::where(function($query) use($filters) {
foreach( $filters as $filter )
$query = $query->orWhere($filter);
})
->where([ADDITIONAL CONSTRAINTS... (like age)])->get();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.