我有..
型号:
Player
id,player_name
Sport
id,sports_name
Game
id,player_id,sports_id,scores
模型关系
玩家 有很多 游戏、体育 有很多 游戏、游戏属于玩家和体育。
问题:在 Controller 中,是否可能加载体育以及每个游戏中的每个< em>玩家?
在单个查询中,我想在我的Blade中实现这样的效果..
@foreach($player as $p)
@foreach ($p->sport as $ps) /*this wont work, since player has not relationship with sports*/
@foreach ($ps->game as $psg)
{{$psg->id}}
{{$psg->player_name}}
{{$psg->sports_name}}
{{$psg->scores}}
@endforeach
@endforeach
@endforeach
还有其他方法可以实现吗?谢谢!
最佳答案
您可以使用Nested eager loading :玩家有很多游戏,游戏属于一项运动。
$player=Player::with('games','games.sport')->find($id);
foreach($player->games as $game)
{
echo $game;
echo $game->sport;
}
关于php - Laravel 5 急切加载没有直接关系的模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40841969/