laravel 循环中的多个 where 子句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19485893/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:34:56  来源:igfitidea点击:

laravel multiple where clauses within a loop

phpmysqllaravellaravel-4

提问by user1424508

Pretty much I want the query to select all records of users that are 25 years old AND are either between 150-170cm OR 190-200cm.

我几乎希望查询选择所有 25 岁且介于 150-170 厘米或 190-200 厘米之间的用户记录。

I have this query written down below. However the problem is it keeps getting 25 year olds OR people who are 190-200cm instead of 25 year olds that are 150-170 OR 25 year olds that 190-200cm tall. How can I fix this? thanks

我在下面写下了这个查询。然而,问题是它不断吸引 25 岁或 190-200 厘米的人,而不是 150-170 岁的 25 岁或 190-200 厘米高的 25 岁人。我怎样才能解决这个问题?谢谢

 $heightarray=array(array(150,170),array(190,200));
 $user->where('age',25);

   for($i=0;$i<count($heightarray);i++){
 if($i==0){
   $user->whereBetween('height',$heightarray[$i])
}else{
   $user->orWhereBetween('height',$heightarray[$i])
 }
 }
      $user->get();

Edit: I tried advanced wheres (http://laravel.com/docs/queries#advanced-wheres) and it doesn't work for me as I cannot pass the $heightarray parameter into the closure.

编辑:我尝试了高级 wheres(http://laravel.com/docs/queries#advanced-wheres),但它对我不起作用,因为我无法将 $heightarray 参数传递到闭包中。

from laravel documentation

来自 Laravel 文档

 DB::table('users')
        ->where('name', '=', 'John')
        ->orWhere(function($query)
        {
            $query->where('votes', '>', 100)
                  ->where('title', '<>', 'Admin');
        })
        ->get();

回答by FR6

Like Jeemusuand the OP stated, you need to use advance wheres. But if you want to pass a variable to the closure function you need to make use of the "use" approach:

就像Jeemusu和 OP 所说的那样,您需要使用提前 wheres。但是如果你想将一个变量传递给闭包函数,你需要使用“ use”方法:

$heightarray = array(array(150,170),array(190,200));

DB::table('users')
  ->where('name', '=', 'John')
  ->orWhere(function($query) use ($heightarray){
    //...
  })
  ->get();

回答by user1424508

I found the answer. I need to include "use" in the closure to pass my $heightarray variable in. Once $heightarray is in then laravel's advance wheres work.

我找到了答案。我需要在闭包中包含“use”以传递我的 $heightarray 变量。一旦 $heightarray 进入,那么 Laravel 的提前在哪里工作。

$heightarray=array(array(150,170),array(190,200));
  $user->where('age',25);

 $userprofile->Where(function($query) use ($heightarray) {

 for($i=0;$i<count($heightarray);i++){
 if($i==0){
 $user->whereBetween('height',$heightarray[$i])
 }else{
 $user->orWhereBetween('height',$heightarray[$i])
  }
}

 });

  $user->get();

回答by Jeemusu

This is completely untested, but looking at the documentation for advance wheres, it would seem you want to try something like this:

这是完全未经测试的,但是查看Advance wheres的文档,您似乎想尝试以下操作:

DB::table('users')
->where('age',25)
->Where(function($query)
{
    for($i=0;$i<count($heightarray);i++){
       if($i==0){
          $query->whereBetween('height', $heightarray[$i]);
       }else{
          $query->orWhereBetween('height', $heightarray[$i]);
       }
    }
})->get();