From 0abf0e4f53486654eca247eb6322721b4774dffe Mon Sep 17 00:00:00 2001 From: Tina Hammar Date: Thu, 21 Dec 2023 13:14:33 +0100 Subject: [PATCH] Belongs to many teams, disabled if user ! team owner --- src/Forms/TeamBelongsToMany.php | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/Forms/TeamBelongsToMany.php b/src/Forms/TeamBelongsToMany.php index d66fa12..ec82830 100644 --- a/src/Forms/TeamBelongsToMany.php +++ b/src/Forms/TeamBelongsToMany.php @@ -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(); } }