2014-09-03 62 views
0

我是一個大的初學者,我做了這麼多的研究,並與即時鬥爭3天現在。如何解決我的Laravel之間查詢相同的列名

我求求你們,請幫我

請我求求你們幫我

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); 
    } 
+0

請幫我修改一下這個邏輯,我發誓我會將所有的點數轉給你 – 2014-09-03 13:24:27

回答

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

我不是進入,不管這個代碼是好還是不好,但是這會爲y做好工作OU。

+0

那會給出錯誤的結果,它會返回我的數據庫的所有結果,但是謝謝你的回答 – 2014-09-03 13:32:25

+0

不,不。顯示您想要執行的查詢以及其他代碼。如果你有另一個'wheres',那麼你需要將這些'orWhereBetweens'封裝爲嵌套OR,如果你知道我的意思。你沒有努力顯示預期的產出,所以這是猜測。 – 2014-09-03 13:35:33

+0

謝謝你,我會做比 – 2014-09-03 13:44:04

相關問題