Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions src/Concerns/HasSelectableValues.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Backstage\Fields\Concerns;

use Filament\Forms\Components\CheckboxList;
use Filament\Forms\Components\Hidden;
use Filament\Forms\Components\Repeater;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
Expand Down Expand Up @@ -132,7 +131,9 @@ protected static function buildRelationshipOptions(mixed $field): array
continue;
}

$opts = $results->pluck($relation['relationValue'] ?? 'name', $relation['relationKey'])->toArray();
// Fallback to model's primary key for existing records that don't have relationKey set
$relationKey = $relation['relationKey'] ?? $model->getKeyName();
$opts = $results->pluck($relation['relationValue'] ?? 'name', $relationKey)->toArray();

if (count($opts) === 0) {
continue;
Expand Down Expand Up @@ -255,8 +256,13 @@ protected function selectableValuesFormFields(string $type, string $label, strin
return [$column => Str::title($column)];
})->toArray();

// Get the primary key of the model
$primaryKey = $model->getKeyName();

$set('relationValue', null);
$set('relationValue_options', $columnOptions);
$set('relationKey_options', $columnOptions);
$set('relationKey', $primaryKey);
})
->options(function () {
$resources = config('backstage.fields.selectable_resources');
Expand All @@ -278,20 +284,23 @@ protected function selectableValuesFormFields(string $type, string $label, strin
fn (Get $get): bool => is_array($get("../../config.{$type}")) && in_array('relationship', $get("../../config.{$type}")) ||
$get("../../config.{$type}") === 'relationship'
),
Select::make('relationValue')
->label(__('Column'))
->helperText(__('The column to use as name for the options'))
->options(fn (Get $get) => $get('relationValue_options') ?? [])
Select::make('relationKey')
->label(__('Key Column'))
->helperText(__('The column to use as the unique identifier/value for each option'))
->options(fn (Get $get) => $get('relationKey_options') ?? [])
->searchable()
->visible(fn (Get $get): bool => ! empty($get('resource')))
->required(fn (Get $get): bool => ! empty($get('resource'))),
Hidden::make('relationKey')
->default('ulid')
->label(__('Key'))
->required(
fn (Get $get): bool => is_array($get("../../config.{$type}")) && in_array('relationship', $get("../../config.{$type}")) ||
$get("../../config.{$type}") === 'relationship'
),
Select::make('relationValue')
->label(__('Display Column'))
->helperText(__('The column to use as the display text/label for each option'))
->options(fn (Get $get) => $get('relationValue_options') ?? [])
->searchable()
->visible(fn (Get $get): bool => ! empty($get('resource')))
->required(fn (Get $get): bool => ! empty($get('resource'))),
Repeater::make('relationValue_filters')
->label(__('Filters'))
->visible(fn (Get $get): bool => ! empty($get('resource')))
Expand Down
Loading