2016-09-06 164 views
0

我有一个主表“Qsos”和一堆关系的项目。现在,当我尝试创建高级搜索时,我不知道如何同时查询所有关系。通联模式有以下:Laravel 4雄辩关系查询

public function band() 
{ 
    return $this->belongsTo('Band'); 
} 

public function mode() 
{ 
    return $this->belongsTo('Mode'); 
} 

public function prefixes() 
{ 
    return $this->belongsToMany('Prefix'); 
} 

public function user() 
{ 
    return $this->belongsTo('User'); 
} 

public function customization() { 
    return $this->hasOne('Customization'); 
} 

然后,我有SearchController与以下有返回的所有类星体下列规定的条件集合代码:

$qsos = Qso::withUser($currentUser->id) 
        ->join('prefix_qso','qsos.id','=','prefix_qso.qso_id') 
        ->join('prefixes','prefixes.id','=','prefix_qso.prefix_id') 
        ->where('prefixes.territory','like',$qTerritory) 
        ->withBand($qBand) 
        ->withMode($qMode) 
        ->where('call','like','%'.$input['qCall'].'%') 
        ->orderBy('qsos.id','DESC') 
        ->paginate('20'); 

然后在视图我需要调用$ qso-> prefixes-> first()和$ qso-> prefixes-> last()(Qso和Prefix具有manyToMany关系),但都返回null。哪里不对?

回答

0

这里是雄辩的代码,我找到工作,但采取非常长的时间来处理:

  $qsos = Qso::withUser($currentUser->id) 
        ->with('prefixes') 
        ->withBand($qBand) 
        ->withMode($qMode) 
        ->where('call','like','%'.$input['qCall'].'%') 
        ->whereHas('prefixes', function($q) use ($qTerritory) { 
         $q->where('territory','like',$qTerritory); 
        }) 
        ->orderBy('qsos.id','DESC') 
        ->paginate('20'); 
+0

如果我取出来拿走了(“前缀”)的速度变得可以接受 – brack11