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;
}