test
test

Reputation: 18200

Laravel - Has Relationship Through

Let's say I have three tables:

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

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

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

How would I use Laravel's hasManyThrough to able to get a list of activity from a server's players like so $system->activity?

Upvotes: 0

Views: 39

Answers (1)

nakov
nakov

Reputation: 14268

You can use it as follows:

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

This will give you all the server activities through the player.

Upvotes: 1

Related Questions