From 6f4a6881f850829b019daf9c942388e47fd1802e Mon Sep 17 00:00:00 2001 From: oliuz Date: Tue, 22 Sep 2020 21:13:12 -0400 Subject: [PATCH] Fixed problem with foreign column of team in users table --- config/config.php | 10 ++++++++++ src/Traits/UserHasTeams.php | 16 ++++++++++------ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/config/config.php b/config/config.php index 03895e8..958f671 100755 --- a/config/config.php +++ b/config/config.php @@ -33,6 +33,16 @@ */ 'users_table' => 'users', + /* + |-------------------------------------------------------------------------- + | Teamwork current team id in users Table + |-------------------------------------------------------------------------- + | + | This is the current team id in users table name used by Teamwork. + | + */ + 'current_team' => 'current_team_id', + /* |-------------------------------------------------------------------------- | Teamwork Team Model diff --git a/src/Traits/UserHasTeams.php b/src/Traits/UserHasTeams.php index 1d41b80..932c32e 100755 --- a/src/Traits/UserHasTeams.php +++ b/src/Traits/UserHasTeams.php @@ -39,7 +39,7 @@ public function teams() */ public function currentTeam() { - return $this->hasOne(Config::get('teamwork.team_model'), 'id', 'current_team'); + return $this->hasOne(Config::get('teamwork.team_model'), 'id', config('teamwork.current_team')); } /** @@ -139,12 +139,13 @@ public function isOwnerOfTeam($team) public function attachTeam($team, $pivotData = []) { $team = $this->retrieveTeamId($team); + $current_team_id = config('teamwork.current_team'); /** * If the user has no current team, * use the attached one */ - if (is_null($this->current_team)) { - $this->current_team = $team; + if (is_null($this->$current_team_id)) { + $this->$current_team_id = $team; $this->save(); if ($this->relationLoaded('currentTeam')) { @@ -176,6 +177,7 @@ public function attachTeam($team, $pivotData = []) */ public function detachTeam($team) { + $current_team_id = config('teamwork.current_team'); $team = $this->retrieveTeamId($team); $this->teams()->detach($team); @@ -189,8 +191,8 @@ public function detachTeam($team) * If the user has no more teams, * unset the current_team_id */ - if ($this->teams()->count() === 0 || $this->current_team === $team) { - $this->current_team = null; + if ($this->teams()->count() === 0 || $this->$current_team_id === $team) { + $this->$current_team_id = null; $this->save(); if ($this->relationLoaded('currentTeam')) { @@ -239,6 +241,8 @@ public function detachTeams($teams) */ public function switchTeam($team) { + $current_team_id = config('teamwork.current_team'); + if ($team !== 0 && $team !== null) { $team = $this->retrieveTeamId($team); $teamModel = Config::get('teamwork.team_model'); @@ -254,7 +258,7 @@ public function switchTeam($team) throw $exception; } } - $this->current_team = $team; + $this->$current_team_id = $team; $this->save(); if ($this->relationLoaded('currentTeam')) {