Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

AManInYorkshire's avatar

3-way many to many relationship with pivot table

Hi all,

I'm after some help querying a many to many relationship. I have three models, Team, Player and LeagueSeason.

A player may be associated with many teams and league seasons.

I have three models and a pivot class representing the pivot table. Note that my pivot table is called squads.

Here are my 3 models and 1 pivot table class: Team.php:

<?php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Team extends Model
{
    protected $fillable = ['name','short_name','official_name'];
    
    public function leagueSeasons()
    {
        return $this->belongsToMany(LeagueSeason::class,'squad')
            ->withPivot('id','league_season_id')
            ->withTimestamps()
            ->using(Squad::class);
    }


    public function players()
    {
        return $this->belongsToMany(Player::class,'squads')
            ->withPivot('id','league_season_id')
            ->withTimestamps()
            ->using(Squad::class);
    }
}

Player.php:

<?php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Player extends Model
{
	protected $fillable = ['first_name', 'last_name', 'alt_name', 
        'date_of_birth', 'place_of_birth', 'height', 'weight', 'team_id'];

    public function teams()
    {
        return $this->belongsToMany(Team::class, 'squads')
            ->withPivot('id','league_season_id')
            ->withTimestamps()
            ->using(Squad::class);
    }

    public function leagueSeason()
    {
        return $this->belongsToMany(LeagueSeason::class, 'squads')
            ->withPivot('id','league_season_id')
            ->withTimestamps()
            ->using(Squad::class);
    }
}

LeagueSeason.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class LeagueSeason extends Model
{
    protected $fillable = [
        'league_id',
        'code',
        'start_date',
        'end_date',
        'name',
        'current_season',
    ];

    public function teams()
    {
        return $this->belongsToMany(Team::class, 'squads')
            ->withPivot('id','league_season_id')
            ->withTimestamps()
            ->using(Squad::class);
    }

    public function players()
    {
        return $this->belongsToMany(Player::class,'squads')
            ->withPivot('id','league_season_id')
            ->withTimestamps()
            ->using(Squad::class);
    }
}

And a pivot table class Squad.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Relations\Pivot;

class Squad extends Pivot
{
    public $incrementing = true;

}

With the above in place. I am able to do something like the following to get all players for a team:

$team = Team::whereName('England')->first();

$players = $team->players()->get();

However, I cannot work out how I would get all players associated with a particular league season, by the league season code property. I can do it as follows:

$team = Team::whereName('England')->first();

$players = $team->players()->where('league_season_id','1974')->get();

But clearly, this requires me to know the ``league_season_id, rather than the codewhich is a property of theLeagueSeason``` model.

Any help with this would be much appreciated.

UPDATED: to include fillable properties

0 likes
1 reply
sergiosrtd's avatar

Can you edit your post and add the fillable properties of the models?

Please or to participate in this conversation.