Skip to content

Commit

Permalink
Belongs to many teams, disabled if user ! team owner
Browse files Browse the repository at this point in the history
  • Loading branch information
tanthammar committed Dec 21, 2023
1 parent 39ac60b commit 0abf0e4
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/Forms/TeamBelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,25 @@ class TeamBelongsToMany
*/
public static function make(): CheckboxList
{
/**
* When using disabled() with relationship(),
* ensure that disabled() is called before relationship().
* This ensures that the dehydrated() call from within relationship()
* is not overridden by the call from disabled()
*/
return CheckboxList::make('teams')->label(__('fields.team'))
->disabled( ! (user()?->isSupport() || user()?->ownsCurrentTeam()) ) //before relationship()
->default([user()->current_team_id]) //non-team owners can only select their own team
->relationship('teams', 'name')
->default([user()->current_team_id])
->options(
fn ($get) => user()?->isSupport()
? User::find($get('user_id'))?->allTeams()->pluck('name', 'id') ?? collect()
: user()?->ownedTeams()->pluck('name', 'id') ?? collect()
? User::find($get('user_id'))?->allTeams()->pluck('name', 'id') ?? collect()
: user()?->ownedTeams()->pluck('name', 'id') ?? collect()
)
->bulkToggleable()
->columns(2)
->required()
->rule('array')
->ruleEachInOptions()
->visible(user()?->isSupport() || user()?->hasOwnedTeams());
->ruleEachInOptions();
}
}

0 comments on commit 0abf0e4

Please sign in to comment.