繁体   English   中英

Laravel-通过建立关系

[英]Laravel - Has Relationship Through

假设我有三个表:

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

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

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

我将如何使用Laravel的hasManyThrough来获取服务器玩家的活动列表,例如$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...
    );
}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM