['relation' => 'contacts', 'label' => 'full_name'], 'location' => ['relation' => 'locations', 'label' => 'name'], 'domain' => ['relation' => 'domains', 'label' => 'name'], ]; protected function casts(): array { return [ 'options' => 'array', 'is_required' => 'boolean', 'show_in_list' => 'boolean', ]; } public function type() { return $this->belongsTo(FlexibleAssetType::class, 'flexible_asset_type_id'); } public function relatedTarget(): ?string { return $this->options['target'] ?? null; } /** * Records available for this field's related_asset target, scoped to the organization. * @return Collection */ public function relatedOptions(Organization $organization): Collection { $target = $this->relatedTarget(); if (! $target) { return collect(); } if (str_starts_with($target, 'flexible_asset:')) { $typeId = (int) substr($target, strlen('flexible_asset:')); return $organization->flexibleAssets() ->where('flexible_asset_type_id', $typeId) ->orderBy('name') ->get() ->map(fn (FlexibleAsset $asset) => ['id' => $asset->id, 'label' => $asset->name]); } $config = self::RELATED_TARGETS[$target] ?? null; if (! $config) { return collect(); } return $organization->{$config['relation']}() ->orderBy($config['label'] === 'full_name' ? 'last_name' : $config['label']) ->get() ->map(fn (Model $record) => ['id' => $record->id, 'label' => (string) $record->{$config['label']}]); } }