如何在相同列名的查询之间解决我的Laravel

时间:2014-09-03 13:21:04

标签: php mysql sql laravel-4

我是一个很大的初学者,我做了很多研究,现在我已经为此奋斗了3天。

我求求你们帮助我们

请求我们帮助我们

I have a ManyToMany relation

public function measurement()
    {
        return $this->belongsToMany('Measurement', 'users_measurement')->withPivot('value');
    }

我的搜索查询

 ->whereHas('measurement', function($q) use ( $from, $to )
        {
              foreach (array_combine($from, $to) as $f => $t) 
              {
                    if(!empty($f) && !empty($t))
                    {
                        $q->whereBetween('users_measuement.value', array($f, $t));
                    }
                }
        })

关系表

user_id | measurement_id | value 
2       | 1              | 165 cm
2       | 2              | 48 kg
1       | 1              | 150 cm
1       | 2              | 35 kg

我希望这可以在同一列中使用不同的值之间使用多于一个

从Laravel调试

返回的示例
lara_users_measurement`.`value` between 151 cm and 155 cm and `lara_users_measurement`.`value` between 35 kg and 52 kg

请帮助我在这3天内苦苦挣扎:(

已编辑:更多详情

表格

{{ Form::open(array('url' => 'users/results', 'method' => 'GET', 'class' => 'ui form')) }}
                <div class="four fields">
                    <div class="field">
                      <label>Vezetéknév</label>
                      <input name="first_name" type="text">
                    </div>
                    <div class="field">
                      <label>Keresztnév</label>
                      <input name="last_name" type="text">
                    </div>
                    <div class="field">
                      <label>Felhasználónév</label>
                      <input name="username" type="text">
                    </div>
                    <div class="field">
                      <label>Helység</label>
                      <input name="location" id="location" type="text">
                    </div>
                 </div>
                 <div class="three fields">
                    <div class="field">
                      <label>Neme</label>
                      <select name="gender" id="" class="form-select">
                        <option value="">Összes</option>
                        <option value="1">Férfi</option>
                        <option value="2">Nő</option>
                      </select>
                    </div>
                    <div class="field">
                      <label>Tag fiók típusa</label>
                      <select name="account" id="account" class="form-select">
                        <option value="">Fiók típus kiválasztása</option>
                         @foreach($accounts as $account)
                            <option value="{{ $account->id }}">{{ $account->name }}</option>
                         @endforeach
                      </select>
                    </div>
                    <div class="field">
                     <label>Munka típus</label>
                      <select name="genres[]" id="genres" class="form-select-multiple" multiple>
                         @foreach($genres as $genre)
                            <option value="{{ $genre->id }}">{{ $genre->name }}</option>
                         @endforeach
                      </select>
                    </div>
                 </div>
                 <div id="measurements-form">
                     <h3>Méretek és egyéb információ</h3>
                        @foreach($measurements as $measurement)
                            <div class="search-fields">
                                <label for="{{ $measurement->id }}">{{ $measurement->name }}</label>
                                <div class="two-fields">
                                  <select name="{{ 'from['.$measurement->id.']' }}" id="{{ $measurement->id }}" >
                                  <option value="">{{ $measurement->name  }} (tól)</option>
                                      @foreach(unserialize($measurement->value) as $value)
                                        <option value="{{ $value }}">{{ $value }}</option>
                                      @endforeach
                                  </select>

                                  <select name="{{ 'to['.$measurement->id.']' }}" id="{{ $measurement->id }}" >
                                  <option value="">{{ $measurement->name  }} (ig)</option>
                                      @foreach(unserialize($measurement->value) as $value)
                                        <option value="{{ $value }}">{{ $value }}</option>
                                      @endforeach
                                  </select>
                                </div>
                            </div>
                        @endforeach  
                 </div>
                 <div class="clearfix"></div>
                 {{ Form::submit('Keres', array('class' => 'ui orange small button')) }}
                {{ Form::close() }}
            </div>

结果控制器

 function __construct(User $user, Comment $comment, Account $account, Genre $genre, Measurement $measurement)
    {
        $this->user = $user;

        $this->comment = $comment;

        $this->account = $account;

        $this->genre = $genre;

        $this->measurement = $measurement; 
    }

public function results()
    {

       $first_name = Input::get('first_name');
       $last_name = Input::get('last_name');
       $gender = Input::get('gender');
       $location = Input::get('location');
       $genre = Input::get('genres');
       $from = Input::get('from');
       $to = Input::get('to');


       $users = $this->user
       ->where('username', 'LIKE',  '%'.Input::get('username').'%')
       ->where('account_id', 'LIKE', '%'.Input::get('account').'%')
       ->whereHas('profile', function($q) use ( $first_name, $last_name, $gender, $location )
        {
            $q->where('first_name', 'LIKE', '%'.  $first_name )
               ->where('last_name', 'LIKE', '%'.  $last_name )
               ->where('gender', 'LIKE', '%'.  $gender )
               ->where('location', 'LIKE', '%'.  $location .'%' );

        })
        ->whereHas('genre', function($q) use ( $genre )
        {
            if(isset($genre))
            {
                $q->whereIn('genre_id', $genre );
            }

        })
         ->whereHas('measurement', function($q) use ( $from, $to )
        {
              foreach (array_combine($from, $to) as $f => $t) 
              {
                    if(!empty($f) && !empty($t))
                    {
                        $q->orWhereBetween('users_measurement.value', array($f, $t));
                    }
                }
        })
       ->where('approved', '=', 1)
       ->where('activated', '=', 1)
       ->paginate(15);

       $users->appends(Input::except('_token'))->links(); 

       $this->layout->title = 'Tagok';
       $this->layout->content = View::make('user::user/index')->with('users', $users);
    }

1 个答案:

答案 0 :(得分:2)

您需要的只是orWhereBetween而不是whereBetween并且包含在where闭包中(添加嵌套的where子句):

->whereHas('measurements', function ($q) use ($from, $to) {
  $q->where(function($q) use ($from, $to) {
    foreach (array_combine($from, $to) as $f => $t)
    {
      if(!empty($f) && !empty($t))
      {
        $q->orWhereBetween('users_measurement.value', array($f, $t));
      }
    } 
  });
})

我没有进入,这段代码是否合适,但这将为你完成工作。