6

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.

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

 $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.

from laravel documentation

 DB::table('users')
        ->where('name', '=', 'John')
        ->orWhere(function($query)
        {
            $query->where('votes', '>', 100)
                  ->where('title', '<>', 'Admin');
        })
        ->get();
4
  • You need to make use of advance wheres. Commented Oct 21, 2013 at 3:46
  • I tried that but I can't pass variables into the closures Commented Oct 21, 2013 at 3:47
  • Can you post the advanced where statement that you tried? Commented Oct 21, 2013 at 3:52
  • I did something simliar Jeemusu in the bottom Commented Oct 21, 2013 at 4:06

3 Answers 3

10

Like Jeemusu and 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:

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

DB::table('users')
  ->where('name', '=', 'John')
  ->orWhere(function($query) use ($heightarray){
    //...
  })
  ->get();
Sign up to request clarification or add additional context in comments.

Comments

2

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.

$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();

Comments

2

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

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();

1 Comment

I tried that but you need to pass heightarray into the closure otherwise it doesn't know what you are talking about

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.