2016-09-06 29 views
0

私はメインテーブル「のQSO」との関係の束でプロジェクトを持っています。今では高度な検索を作成しようとすると、同時にどのようにすべてのリレーションをクエリするかわかりません。 QSOモデルは、次のい: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'); 
} 

をその後、私は必要な条件以下のすべてのQSOのコレクションを返すために持って、次のコードで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->最初の()と$ qso-> prefixes->最後の()(QSOとプレフィックスは、多対多の関係を持っている)が、両方のリターンは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