Mpociot\Teamwork\Traits\UserHasTeams::attachTeam PHP 메소드

attachTeam() 공개 메소드

Alias to eloquent many-to-many relation's attach() method.
public attachTeam ( mixed $team, array $pivotData = [] )
$team mixed
$pivotData array
    public function attachTeam($team, $pivotData = [])
    {
        $team = $this->retrieveTeamId($team);
        /**
         * If the user has no current team,
         * use the attached one
         */
        if (is_null($this->current_team_id)) {
            $this->current_team_id = $team;
            $this->save();
            if ($this->relationLoaded('currentTeam')) {
                $this->load('currentTeam');
            }
        }
        // Reload relation
        $this->load('teams');
        if (!$this->teams->contains($team)) {
            $this->teams()->attach($team, $pivotData);
            event(new UserJoinedTeam($this, $team));
            if ($this->relationLoaded('teams')) {
                $this->load('teams');
            }
        }
        return $this;
    }