Skip to content

Commit

Permalink
Allow dev to apply pivot data on attaching a team.
Browse files Browse the repository at this point in the history
Signed-off-by: Micheal Mand <[email protected]>
  • Loading branch information
mikemand committed May 24, 2016
1 parent a009a11 commit e674396
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/Mpociot/Teamwork/Contracts/TeamworkUserInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ public function isOwnerOfTeam( $team );
* Alias to eloquent many-to-many relation's attach() method.
*
* @param mixed $team
* @param array $pivotData
*/
public function attachTeam( $team );
public function attachTeam( $team, $pivotData = [] );

/**
* Alias to eloquent many-to-many relation's detach() method.
Expand Down
5 changes: 3 additions & 2 deletions src/Mpociot/Teamwork/Traits/UserHasTeams.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,10 @@ public function isOwnerOfTeam( $team )
* Alias to eloquent many-to-many relation's attach() method.
*
* @param mixed $team
* @param array $pivotData
* @return $this
*/
public function attachTeam( $team )
public function attachTeam( $team, $pivotData = [] )
{
$team = $this->retrieveTeamId( $team );
/**
Expand All @@ -155,7 +156,7 @@ public function attachTeam( $team )

if( !$this->teams->contains( $team ) )
{
$this->teams()->attach( $team );
$this->teams()->attach( $team, $pivotData );

if( $this->relationLoaded('teams') ) {
$this->load('teams');
Expand Down
18 changes: 18 additions & 0 deletions tests/UserHasTeamsTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,24 @@ public function testCanAttachTeamAsIDToUser()
$this->assertEquals(TeamworkTeam::find(1)->toArray(), $this->user->currentTeam->toArray());
}

public function testCanSetPivotDataOnAttachTeamMethod()
{
\Schema::table(config( 'teamwork.team_user_table' ), function ($table) {
$table->boolean('pivot_set')->default(false);
});

$team = TeamworkTeam::create(['name' => 'Test-Team']);
$pivotData = ['pivot_set' => true];

$this->user->attachTeam($team, $pivotData);

$this->seeInDatabase(config('teamwork.team_user_table'), [
'user_id' => $this->user->getKey(),
'team_id' => $team->getKey(),
'pivot_set' => true
]);
}

public function testIsTeamOwner()
{
$team = TeamworkTeam::create(['name' => 'Test-Team']);
Expand Down

0 comments on commit e674396

Please sign in to comment.