diff --git a/app/Models/Ability.php b/app/Models/Ability.php index f072bafcca..c554f6b75c 100644 --- a/app/Models/Ability.php +++ b/app/Models/Ability.php @@ -210,23 +210,16 @@ public function entryWithAttributes() public function attachEntity(array $request): int { $entityIds = Arr::get($request, 'entities'); - $entities = Entity::with('abilities')->findOrFail($entityIds); $count = 0; - foreach ($entities as $entity) { - // Make sure the tag isn't already attached to the entity - foreach ($entity->abilities as $ability) { - if ($ability->ability_id == $this->id) { - continue; - } - } + $visibility = Arr::get($request, 'visibility_id', \App\Enums\Visibility::All); + $sync = []; - EntityAbility::create([ - 'ability_id' => $this->id, - 'entity_id' => $entity->id, - 'visibility_id' => Arr::get($request, 'visibility_id', \App\Enums\Visibility::All), - ]); - $count++; + foreach ($entityIds as $entity) { + $sync[$entity] = ['visibility_id' => $visibility]; + $count++; } + $this->entities()->syncWithoutDetaching($sync); + return $count; } diff --git a/resources/views/abilities/entities/_form.blade.php b/resources/views/abilities/entities/_form.blade.php index 1d8668babb..164c446ca8 100644 --- a/resources/views/abilities/entities/_form.blade.php +++ b/resources/views/abilities/entities/_form.blade.php @@ -3,7 +3,12 @@ - @include('components.form.entities', ['options' => ['exclude-entity' => $model->entity->id]]) + + @include('cruds.fields.visibility_id', ['model' => null]) diff --git a/resources/views/components/form/entities.blade.php b/resources/views/components/form/entities.blade.php index d31ee32a14..c5dcaf034a 100644 --- a/resources/views/components/form/entities.blade.php +++ b/resources/views/components/form/entities.blade.php @@ -5,11 +5,9 @@ * @var \App\Models\MiscModel $model */ $selectedOption = []; - $model = Arr::get($options, 'model', null); $enableNew = Arr::get($options, 'enableNew', true); $label = Arr::get($options, 'label', true); - // From source to exclude duplicates $searchParams = [$campaign]; if (Arr::has($options, 'exclude', false)) { @@ -17,7 +15,6 @@ } elseif (Arr::has($options, 'exclude-entity', false)) { $searchParams['exclude-entity'] = Arr::get($options, 'exclude-entity'); } - // Try to load what was sent with the form first, in case there was a form validation error $previous = old('entities[]'); if (!empty($previous)) { @@ -36,12 +33,12 @@ id="{{ Arr::get($options, 'id', 'entities[]') }}" class="w-full form-tags form-entities" style="width: 100%" - data-url="{{ route('search.ability-entities', $searchParams) }}" + data-url="{{ route(Arr::get($options, 'route', 'search.ability-entities'), $searchParams) }}" data-allow-new="{{ $enableNew ? 'true' : 'false' }}" data-allow-clear="{{ Arr::get($options, 'allowClear', 'true') }}" data-new-tag="{{ __('entities.new_entity') }}" data-placeholder="{{ __('entities/relations.placeholders.target') }}" - :dropdownParent="$dropdownParent ?? (request()->ajax() ? '#primary-dialog' : null)" + :dropdownParent="$dropdownParent ?? ($ajax ? '#primary-dialog' : null)" > @foreach ($selectedOption as $key => $entity)