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

bluedreamer's avatar

How to set multiple attributes when setting a hasMany model

I have the following models, SportClub, SportLeague, LeagueTeam, TeamMember.

A SportClub hasMany SportLeague's

A SportLeague hasMany LeagueTeam's

A LeagueTeam hasMany TeamMember's

SportLeague has sport_club_id

LeagueTeam has sport_club_id, sport_league_id

TeamMember has sport_club_id,sport_league_id,league_team_id

When I do:

$league_team->teamMembers()->save($team_member);

How can I make save() set sport_club_id and sport_league_id on $team_member from $league_team just like it automatically sets league_team_id.

0 likes
3 replies
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

To automatically set the sport_club_id and sport_league_id on the TeamMember model when saving it through the LeagueTeam model, you can use Eloquent model events or override the save method in the TeamMember model. Here's a solution using model events:

  1. Using Model Events:

    You can listen to the creating event of the TeamMember model to set the additional attributes before the model is saved to the database.

    First, ensure you have the necessary relationships defined in your models:

    // LeagueTeam model
    public function teamMembers()
    {
        return $this->hasMany(TeamMember::class);
    }
    
    // TeamMember model
    public function leagueTeam()
    {
        return $this->belongsTo(LeagueTeam::class);
    }
    

    Then, in the TeamMember model, you can set up the event listener:

    use Illuminate\Database\Eloquent\Model;
    
    class TeamMember extends Model
    {
        protected static function boot()
        {
            parent::boot();
    
            static::creating(function ($teamMember) {
                if ($teamMember->leagueTeam) {
                    $teamMember->sport_club_id = $teamMember->leagueTeam->sport_club_id;
                    $teamMember->sport_league_id = $teamMember->leagueTeam->sport_league_id;
                }
            });
        }
    }
    
  2. Using the save Method Override:

    Alternatively, you can override the save method in the TeamMember model to set these attributes before saving:

    use Illuminate\Database\Eloquent\Model;
    
    class TeamMember extends Model
    {
        public function save(array $options = [])
        {
            if ($this->leagueTeam) {
                $this->sport_club_id = $this->leagueTeam->sport_club_id;
                $this->sport_league_id = $this->leagueTeam->sport_league_id;
            }
    
            return parent::save($options);
        }
    }
    

With either approach, when you call $league_team->teamMembers()->save($team_member);, the sport_club_id and sport_league_id will be automatically set based on the LeagueTeam model.

martinbean's avatar

@bluedreamer Hey! I think you’re maybe working on a project similar to one that I worked on in the past 🙂

I don’t really have an answer to the question at hand. I just have code that sets relevant relationships on a model before saving. If this becomes a bit of a chore, then I’ll extract it to a dedicated method in some sort of service class or similar. But usually, I don’t need to set lots of relationships; it’s usually a case of attaching one model to another.

For example, where you attach a team to a league, I’d just have an addTeam method somewhere where I pass the team I wish to attach:

$league->addTeam($team);

The relationships between the two would be a many-to-many, as a league would have many teams, but a team could compete in many leagues. For example, Real Madrid compete in La Liga in Spain, but also the EUFA Champions League.

Happy for you to reach out to me directly to chat more about your project. I worked for a company that operated a platform for sports clubs and leagues, so have know the area quite well, as well as done a fair bit of modelling of entities and relationships in the space. So happy to share what knowledge I have to help out.

1 like
bluedreamer's avatar

@martinbean Thanks for the offer. I have a lot of dimensions on the data to make querying easier though. I can see missing a call to the addX() function instead of putting it in the model save. At the lowest level I have StationPairScore has sport_club_id, sport_league_id, league_team_id, team_member_id, league_round_id, league_round_station_id, league_round_station_pair_id

Please or to participate in this conversation.