假设我有三个表:
1. Server
-------
id
name
2. Player
-------
id
name
ServerId
3. Activity
-------
id
PlayerId
action
time
我将如何使用Laravel的hasManyThrough来从服务器的玩家那里获取活动列表,例如$system->activity
?
答案 0 :(得分:1)
您可以按以下方式使用它:
// this should be inside your Server model.
public function activities()
{
return $this->hasManyThrough(
Player::class,
Activity::class,
'PlayerId', // Foreign key on activities table...
'ServerId', // Foreign key on players table...
'id', // Local key on servers table...
'id' // Local key on activities table...
);
}
这将通过播放器为您提供所有服务器活动。