[英]how to get opposite results from a query Laravel
How to get the opposite results from a SELECT query 如何从SELECT查询中获得相反的结果
Below is my query 以下是我的查询
$fod = FodMap::select('*')
->where('fructose_level', $fructose == 1 ? '=' : '>=', $fructose)
->where('lactose_level', $lactose == 1 ? '=' : '>=', $lactose)
->where('polyols_level', $polyols == 1 ? '=' : '>=', $polyols)
->where('fructan_level', $fructan == 1 ? '=' : '>=', $fructan)
->get();
I want to get everything that doesn't fall under the above query. 我想获取不属于上述查询的所有内容。 pls advice
请咨询
For making negative statement in Laravel use whereNotIn
expression. 为了在Laravel中做出否定陈述,请使用
whereNotIn
表达式。
Ok, I thought about your question for some time and I think the solution is not as complicated as you asked first (when I've read it first, I thought you want inverted result). 好的,我考虑了一段时间后,我认为解决方案并不像您首先提出的那样复杂(当我首先阅读它时,我认为您想要倒转的结果)。
above results gives the list of foods which user can't eat.. what i want it to get the results which not matching above
上面的结果给出了用户不能吃的食物的列表..我希望它得到上面不匹配的结果
So, solution is way too simple then: 因此,解决方案太简单了:
$fod = FodMap::select('*')
->where('fructose_level', '<', $fructose)
->where('lactose_level', '<', $lactose)
->where('polyols_level', '<', $polyols)
->where('fructan_level', '<', $fructan)
->get();
Also, ternary operators in your original query do not make sense, because clause like: 同样,原始查询中的三元运算符也没有意义,因为子句如下:
->where('fructose_level', $fructose == 1 ? '=' : '>=', $fructose)
will always work as: 将始终作为:
->where('fructose_level', '>=', $fructose)
Think about it. 想一想。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.