2016-05-30 80 views
3

關係我有一個表:訂單通過在口才

  1. 分析,列(id, name, game_id(FK));
  2. 遊戲,欄目(​​);
  3. 圓形,列(id, round);

我需要通過(round_id)獲取分析順序的所有記錄。

我試試Analyses::orderBy('round_id')->get(),但不行;

+0

什麼是T hrown錯誤,輸出是什麼以及想要的結果是什麼? –

+0

你還沒有在分析表中有round_id! ,你的意思是你希望每個分析由round_id排序的遊戲? –

+0

然後,你可以簡單地加入分析和遊戲表,然後按順序'round_id' –

回答

2

首先,你沒有該列在您的分析表

做你必須把它添加到您的關係排序依據(假設他們都做得正確)

分析模型

public function games() 
{ 
    return $this->hasMany(Game::class)->orderBy('round_id'); //see? we can add orderBy to the relation 
} 

遊戲模式

public function rounds() 
{ 
    return $this->hasMany(Round::class); // i dont know if its manytomany for eal, im just trying to explain 
} 

現在,你可以得到所有與遊戲分析和繞行eagerloading這樣

$test = Analyses::with('games.round')->get(); 

您可以鏈接在上面爲例另一排序依據爲這樣的分析對於爲例

$test = Analyses::with('games.round')->orderby('game_id')->get(); 

,你將根據game_id排序分析,並且每個Analyze中的遊戲將按順序排序round_id

我希望這是您想要做的事

+0

是的,關係存在於模型中,我試過你說的但是也行不通,返回那個列不存在的錯誤。 –

+0

告訴我們你做了什麼?你改變了什麼! ,將你的關係代碼粘貼在答案中,請幫助更容易 –

0
Set the following relationships in to the respective model as given below 

分析模型

回合型號

public function games() 
{ 
    return $this->hasMany('Game','game_id','id'); 
} 

博弈模型

public function analyses() 
{ 
    return $this->hasMany('Analyses','round_id','id'); 
} 

您的查詢應該是這樣的

$round_game_wise_analyses= Round::with(['game'=>function($query){ 
           $query->select() 
           ->with(['analyses'=>function($query){ 
              $query->select(); 
             }]); 
           }])->orderBy('id','asc')->get();