Skip to content

Commit 2025fa0

Browse files
authored
feat: let user change the relationKey (#38)
* feat: let user change the relationKey * add backwards compatibility
1 parent 341d6d6 commit 2025fa0

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

src/Concerns/HasSelectableValues.php

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Backstage\Fields\Concerns;
44

55
use Filament\Forms\Components\CheckboxList;
6-
use Filament\Forms\Components\Hidden;
76
use Filament\Forms\Components\Repeater;
87
use Filament\Forms\Components\Select;
98
use Filament\Forms\Components\TextInput;
@@ -132,7 +131,9 @@ protected static function buildRelationshipOptions(mixed $field): array
132131
continue;
133132
}
134133

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

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

259+
// Get the primary key of the model
260+
$primaryKey = $model->getKeyName();
261+
258262
$set('relationValue', null);
259263
$set('relationValue_options', $columnOptions);
264+
$set('relationKey_options', $columnOptions);
265+
$set('relationKey', $primaryKey);
260266
})
261267
->options(function () {
262268
$resources = config('backstage.fields.selectable_resources');
@@ -278,20 +284,23 @@ protected function selectableValuesFormFields(string $type, string $label, strin
278284
fn (Get $get): bool => is_array($get("../../config.{$type}")) && in_array('relationship', $get("../../config.{$type}")) ||
279285
$get("../../config.{$type}") === 'relationship'
280286
),
281-
Select::make('relationValue')
282-
->label(__('Column'))
283-
->helperText(__('The column to use as name for the options'))
284-
->options(fn (Get $get) => $get('relationValue_options') ?? [])
287+
Select::make('relationKey')
288+
->label(__('Key Column'))
289+
->helperText(__('The column to use as the unique identifier/value for each option'))
290+
->options(fn (Get $get) => $get('relationKey_options') ?? [])
285291
->searchable()
286292
->visible(fn (Get $get): bool => ! empty($get('resource')))
287-
->required(fn (Get $get): bool => ! empty($get('resource'))),
288-
Hidden::make('relationKey')
289-
->default('ulid')
290-
->label(__('Key'))
291293
->required(
292294
fn (Get $get): bool => is_array($get("../../config.{$type}")) && in_array('relationship', $get("../../config.{$type}")) ||
293295
$get("../../config.{$type}") === 'relationship'
294296
),
297+
Select::make('relationValue')
298+
->label(__('Display Column'))
299+
->helperText(__('The column to use as the display text/label for each option'))
300+
->options(fn (Get $get) => $get('relationValue_options') ?? [])
301+
->searchable()
302+
->visible(fn (Get $get): bool => ! empty($get('resource')))
303+
->required(fn (Get $get): bool => ! empty($get('resource'))),
295304
Repeater::make('relationValue_filters')
296305
->label(__('Filters'))
297306
->visible(fn (Get $get): bool => ! empty($get('resource')))

0 commit comments

Comments
 (0)