Php Laravel 4无法输出复杂模型结构的数据
第一篇文章是这样写的: 我正在与Laravel 4建立一个统计网站,并在几个模型之间建立了关系- 玩家、游戏、团队、玩家数据、状态类型 所有这些都有相应的表格: 球员:id、姓名、球队idPhp Laravel 4无法输出复杂模型结构的数据,php,sql,laravel-4,eloquent,Php,Sql,Laravel 4,Eloquent,第一篇文章是这样写的: 我正在与Laravel 4建立一个统计网站,并在几个模型之间建立了关系- 玩家、游戏、团队、玩家数据、状态类型 所有这些都有相应的表格: 球员:id、姓名、球队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,
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,统计id 1,值2,游戏1,球员1,统计id 2,值10,游戏1)
因此,当有人想在player 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
模型链接如下:
小组:
玩家:
public function team() {
return $this->belongsTo('Team');
}
public function playerData(){
return $this->hasMany('PlayerData');
}
游戏:
玩家数据:
public function statType(){
return $this->belongsTo('StatType', 'stat_id', 'id');
}
public function game(){
return $this->belongsTo('Game');
}
状态类型:
public function playerData(){
return $this->hasMany('PlayerData');
}
在模型中声明关系时,应指定与模型相关的内容。在您的代码中,我至少看到一个这样的示例:
// 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
访问实例的关系,并能够迭代许多团队
实例
另一个问题是,Elount在声明关系时使用其他默认参数时依赖于约定。如果我想关联两个模型,比如Modela
和Modelb
,那么它假定表名为Modela
和Modelb
。此外,链接列将假定为modelbu id
,例如,在modela
表中
您可以覆盖此行为(您至少需要为您的游戏关系执行此操作,因为您有主队id
和客队id
)。我建议您参考,但这里有一个例子:
// Game model
public function homeTeam()
{
return $this->hasOne('Team', 'home_team_id');
}
请注意,我还将您的belongTomany
更改为hasOne
,我认为这对您更有效,更符合您的目标。首先,您的关系是错误的。以下是它们的外观:
// 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\u 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>
//控制器
$player=player::find($id);
$stats=$player->stats->groupBy('pivot.game_id');
//看法
@foreach($games作为$game)
-
{{$game->homeTeam->name}vs{{{$game->awayTeam->name}
@foreach($stats->get($game->id)作为$stat)
{{$stat->name}:{{$stat->pivot->stat\u value}
@endforeach
@endforeach
感谢您的回复,我的游戏模型和团队模型(仍然错误地)被设置为相互关联,而不是相互关联,我只是在办公室里复制和粘贴的能力很差。我明白你的意思,所以我会根据你的建议更新模型,并尝试实现我的目标,尽管我仍然100%确定我的foreach应该是什么样子,以使桌子看起来像我想要的?干杯。我知道你想要什么,但是$games没有定义。我是应该把控球员的所有比赛都传过来,还是我已经在做什么了?因为如果我传入$team_fixtures(参见OP)并将foreach$team_fixtures作为$game,它会说$game->stats->get($game->id)是一个无效的foreach参数?$games
在我的示例中与您的$team_fixtures
相同,我只是使用了模型的名称,因此更容易阅读。然后,在$game
上没有stats
,stats
是$player
-嵌套foreach上的一个关系。啊,对了,我想我明白你的意思了-在游戏和玩家之间有一个数据透视表(我想我真的应该遵循Laravel命名约定,去掉“玩家数据”)并使用多对多关系来获得与玩家相关的统计数据。确切地说,你的player_数据
是链接player-game-stat的数据透视表。在这种情况下,最好的名称可能是外观
或类似的东西,而不一定是game_-player_-stat
(雄辩的约定),但是player\u data
肯定是个坏名字。我仍然无法让它工作。团队名称很好,但我一直得到“为foreach提供的参数无效”$玩家->统计->获取($game->id)并不会产生所有的统计数据,它只是从统计类型表中检索到一行数据(以及数据透视表中的相关信息),而不是数组-{“id”:2,“统计类型”:“助攻”,“操作id”:“2”,“得分权重”:“5”,“数据透视”:{“玩家id”:1,“统计类型id”:2,“游戏id”:2,“统计值”:2}所以它是在stat_type_id=游戏id的情况下获取stat值,而不是获取游戏的所有统计信息?
// Game model
public function homeTeam()
{
return $this->hasOne('Team', 'home_team_id');
}
// 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');
}
// 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>