2014-09-04 28 views
1

第一篇文章所以在這裏輸出數據雲:Laravel 4無法爲複雜的模型結構

我建立一個統計網站,Laravel 4,並已建立了幾個模型之間的關係 - 播放機,遊戲,團隊,PlayerData ,StatType標識

所有這些都有相應的表格: 球員:ID,姓名,TEAM_ID

games: id, home_team_id, away_team_id, week (note 2 teams in a single game) 
teams: id, name 
stat_types: id, name 
player_datas: id, player_id, stat_type_id, stat_value, game_id 

的想法是,每個球員效力於一支球隊,一個星期誰扮演一次,每次統計的玩家進入每個遊戲將在玩家數據表中具有一個條目(例如,玩家1,stat_id 1,值2,遊戲1,玩家1,stat_id 2,值10,遊戲1)

所以我想要做的是輸出一個表,當有人想要查看播放器玩家show.blade.php( *代表佔位符):

****更新:我已經得到了我想要通過使觀察者的修改建議,但得到的第二和第三細胞出現數據像下面(在我看來)似乎效率低下?覺得我失去了一些東西

@foreach($team_fixtures as $team_fixture) 

<tr> 
    <td>{{$team_fixture->homeTeam->team_name}} vs {{$team_fixture->awayTeam->team_name}}</td> 
    <td>{{$team_fixture->playerData()->where('player_id', $player->id) 
    ->where('stat_type_id', '1') 
    ->pluck('stat_value')}}</td> 
    <td>{{$team_fixture->playerData()->where('player_id', $player->id) 
    ->where('stat_type_id', '2') 
    ->pluck('stat_value')}}</td> 
</tr> 
@endforeach 

我不能告訴我是否錯過了透視表(player_data_stat_types?)或者已經拿到了關係,錯了嗎?如果有更好的結構方式,我會很樂意這樣做,但我不確定從哪一個開始。我開始爲每個$ team_fixtures做一次,但無法獲得輸出的統計信息。我的問題是,夾具左欄,但球員數據表有針對game_id多個條目...

我的播放器看起來像:

public function show($id) 
{ 
    $player = Player::findOrFail($id); 
    $team_fixtures = Game::where('home_team_id', '=', $player->team_id) 
        ->orWhere('away_team_id', '=', $player->team_id) 
        ->get(); 

    return View::make('site/players.show', compact('player', 'team_fixtures')); 
} 

和模型鏈接如下: 團隊:

public function players() { 
    return $this->hasMany('Player'); 
} 

public function games() { 
    return $this->belongsToMany('Game'); 
} 

球員:

public function team() { 
    return $this->belongsTo('Team'); 
} 

public function playerData(){ 
    return $this->hasMany('PlayerData'); 
} 

遊戲:

public function playerData() { 
    return $this->hasMany('PlayerData'); 
} 

public function homeTeam() 
{ 
return $this->hasOne('Team', 'id', 'home_team_id'); 
} 

public function awayTeam() 
{ 
return $this->hasOne('Team', 'id', 'away_team_id'); 
} 

public function playerData(){ 
    return $this->belongsTo('Player', 'player_id', 'id'); 
} 

PlayerData:

public function statType(){ 
    return $this->belongsTo('StatType', 'stat_id', 'id'); 
} 

public function game(){ 
    return $this->belongsTo('Game'); 
} 

StatType標識:

public function playerData(){ 
    return $this->hasMany('PlayerData'); 
} 

回答

0

冷杉,你們的關係是錯誤的。下面是他們應該怎麼看起來像:

// Player 
public function team() 
{ 
    return $this->belongsTo('Team'); 
} 

public function stats() 
{ 
    return $this->belongsToMany('StatType', 'player_datas') 
     ->withPivot('game_id', 'stat_value'); 
} 


// Team 
public function players() 
{ 
    return $this->hasMany('Player'); 
} 

public function awayGames() 
{ 
    return $this->hasMany('Game', 'away_team_id'); 
} 

public function homeGames() 
{ 
    return $this->hasMany('Game', 'home_team_id'); 
} 


// Game 
public function homeTeam() 
{ 
    return $this->belongsTo('Team', 'home_team_id'); 
} 

public function awayTeam() 
{ 
    return $this->belongsTo('Team', 'away_team_id'); 
} 

public function players() 
{ 
    return $this->belongsToMany('Player', 'player_datas') 
     ->withPivot('stat_value', 'stat_type_id'); 
} 

我不認爲你需要PlayerData模式可言的,我想你的stat_types表有name,而不是value領域。

然後,實現你想要的,即。表明用戶對每場比賽的表現(統計值),你想是這樣的:

// controller 
$player = Player::find($id); 
$stats = $player->stats->groupBy('pivot.game_id'); 

// view 
<ul> 
    @foreach ($games as $game) 
     <li> 
     {{ $game->homeTeam->name }} vs {{ $game->awayTeam->name }} 
     <table> 
      @foreach ($stats->get($game->id) as $stat) 
      <tr><td>{{ $stat->name }}: </td><td>{{ $stat->pivot->stat_value }}</td> 
      @endforeach 
     </table> 
     </li> 
    @endforeach 
</ul> 
+0

乾杯。我明白你要做什麼,但是$遊戲沒有定義。我應該只是傳遞來自控制器的所有遊戲或我已經在做什麼?因爲如果我傳入$ team_fixtures(請參閱OP)並且將$ team_fixtures作爲$ game來執行,那麼它表示$ game-> stats-> get($ game-> id)是一個無效的foreach參數? – ExoticChimp 2014-09-05 06:33:43

+0

在我的示例中'$ games'與您的'$ team_fixtures'完全相同,我只是使用了模型的名稱,所以它更易於閱讀。然後,'$ game'上沒有'stats','stats'是'$ player' - 嵌套的foreach中的一個關係。 – 2014-09-05 08:38:19

+0

啊對,我想我明白你的意思了 - 在遊戲和玩家之間有一個數據透視表(我想我應該遵循Laravel的命名規則,擺脫'player_datas'?)並使用多對多關係來獲得與玩家相關的統計信息 – ExoticChimp 2014-09-05 13:28:58

0

當你聲明模型中的關係,你應該指明您的模型有關。在你的代碼中,我看到的這至少一個例子:

// Game model 
public function teams() { 
    return $this->belongsToMany('Game'); 
} 

這確實應該與你的遊戲模式,團隊模式,但你在這裏與遊戲的遊戲。嘗試了這一點:

public function teams() { 
    return $this->belongsToMany('Team'); 
} 

在簡單的設置,使用它可以在模型的實例,使用屬性關係的方法名稱,它沒有其他特殊意義。這也將工作:

public function chicken() { 
    return $this->belongsToMany('Team'); 
} 

有了這個,我可以通過使用$game->chicken訪問實例之間的關係,並能遍歷許多Team實例。

另一個問題是Eloquent在聲明關係時使用其他默認參數時依賴於約定。如果我想要關聯兩個模型,說ModelaModelb,那麼它假定表名將是modelamodelb。此外,連接列將假定爲modelb_id,例如,在modela表內。

您可以覆蓋此行爲(至少對於您的Game關係您至少需要執行此操作,因爲您有home_team_idaway_team_id)。我是指你的documentation,但這裏有一個例子:

// Game model 
public function homeTeam() 
{ 
    return $this->hasOne('Team', 'home_team_id'); 
} 

通知我也改變了你的belongsToManyhasOne,我認爲這將更好地爲你,更符合您要完成的任務。

+0

感謝您的答覆,其實我有我的遊戲和團隊模式(仍然錯誤地)設立的相互關係,而不是他們自己,我只是在辦公室複製粘貼。 我明白你的意思,所以我會根據你的建議更新模型,並盡力達到我的目標,儘管我仍然100%確定我的foreach應該是什麼樣子讓桌子看起來像像我想要的? – ExoticChimp 2014-09-04 17:31:56