mpociot / teamwork

User to Team associations with invitation system for the Laravel 5 Framework
MIT License
1.07k stars 170 forks source link

detachAllTeams and detachAllUsers #9

Closed ziming closed 9 years ago

ziming commented 9 years ago

Hi,

I'm not sure if this feature is already supported but I think something like detachAllTeams or detachAllUsers would be useful when I'm only allowing 1 person to belong to 1 team. So that on my edit team page, I can either remove everyone in team X instantly or get all users currently belonging to team X, remove all their teams associations then grab the form select submission and assign those to team X

mpociot commented 9 years ago

Hi,

On your user model use:

$user->teams()->sync([]);

And for your team:

$team->users()->sync([]);

To remove all related models.

ziming commented 9 years ago

Ah thank you, gonna try it soon and just wondering if I do either of the above and the user now has no team, does current_team_id automatically become null?

mpociot commented 9 years ago

No, you would still need to call $user->switchTeam( null ); in order to reset the current team.

ziming commented 9 years ago

Thank you. Another question.

If I do

$team->users()->sync($listOfUserIds);

If it is the users first team, do their current_team_id get updated with that team id?

mpociot commented 9 years ago

No, the sync method is the standard Laravel behavior. You would need to take care of this yourself.