Php 拉雷维尔-通过

Php 拉雷维尔-通过,php,database,laravel,Php,Database,Laravel,假设我有三张桌子: 1. Server ------- id name 2. Player ------- id name ServerId 3. Activity ------- id PlayerId action time 我如何使用Laravel从服务器的玩家那里获取活动列表,如so$system->activity?您可以按如下方式使用它: // this should be inside your Server model. public function activities

假设我有三张桌子:

1. Server
-------
id
name

2. Player
-------
id
name
ServerId

3. Activity
-------
id
PlayerId
action
time

我如何使用Laravel从服务器的玩家那里获取活动列表,如so
$system->activity

您可以按如下方式使用它:

// 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...
    );
}

这将通过播放机为您提供所有服务器活动。

第一个参数将是您的最终模型,而第二个参数将是您的中间模型。