visible(function (BaseRelationManager $livewire) { if (! $livewire->getRelationship() instanceof BelongsToMany) { return false; } $ownerRecord = $livewire->getOwnerRecord(); $gate = Gate::getPolicyFor($ownerRecord); $ability = Str::of('attachAny') ->append(Str::studly(class_basename($livewire->getTable()->getModel()))) ->__toString(); return (is_object($gate) & method_exists($gate, $ability)) !== 0 ? Gate::forUser(Auth::user())->check($ability, $ownerRecord::class) : true; }); $this->recordSelect(function (BaseRelationManager $livewire): Select { $model = $livewire->getTable()->getModel(); $title = $livewire->getTable()->getRecordTitle(new $model); $select = Select::make('recordId') ->label($title) ->useScout($livewire, $model) ->required(); if ($this->shouldShowCreateOption($model)) { return $select ->createOptionForm(fn (Schema $schema) => Filament::getModelResource($model)::form($schema)->getComponents()) ->createOptionUsing(fn (array $data) => $model::query()->create($data)->getKey()); } return $select; }); $this->schema(fn (AttachAction $action, BaseRelationManager $livewire): array => [ $action->getRecordSelect(), ...$livewire->getPivotComponents(), ]); } /** * Determine whether the create option should be shown. * * @param class-string $model */ private function shouldShowCreateOption(string $model): bool { return $model !== Image::class; } }