Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

davidmarsmalow's avatar

Filament 3 Form Select getOptionLabelFromRecordUsing() showing id instead of the specified label on Resource Edit page

In my form schema where add the Select Component, I want to show the label of the selection with its name. And I have successfully accomplished it on my Create page where it does shows the label of my Room Name label instead of the ID. But I wonder why on the Edit page, where usually the Select component is already selecting one of its option, it shows the ID instead of Room Name label just like from the Create page, and when i open the Select Box it is correctly loading the name and not the ID.

So I wonder why does the selected option in the Edit Page is showing ID and not the given label of Room Name?

Forms\Components\Select::make('room_id')
					->relationship('room', 'id', modifyQueryUsing: fn(Builder $query) => $query->whereDoesntHave('rentals', fn(Builder $q) => $q->where('status', RentalStatus::ACTIVE)))
					->getOptionLabelFromRecordUsing(fn($record) => $record->name)
					->searchable()
					->preload()
					->required()
					->disabledOn('edit')
					->label('Room'),
				Forms\Components\Select::make('tenant_id')
					->relationship('tenant', 'id', modifyQueryUsing: fn(Builder $query) => $query->whereDoesntHave('rentals', fn(Builder $q) => $q->where('status', RentalStatus::ACTIVE)))
					->getOptionLabelFromRecordUsing(fn($record) => $record->user->name)
					->searchable()
					->preload()
					->required()
					->disabledOn('edit')
					->label('Tenant'),

Here is the Edit Page view: https://imgur.com/a/TBJJTwD

Any help would be appreciated. If you want any more information, please let me know. Thanks a lot.

0 likes
2 replies
Rebwar's avatar
Rebwar
Best Answer
Level 32

@davidmarsmalow When you create a record and choose a room, everything works fine because the room has no active rentals yet.

But after saving, that room now has a rental linked to it. So when you go to edit the record, your modifyQueryUsing filter hides that room (since it now has a rental). This makes the select unable to find it — and instead of showing the room name, it just shows the ID.

To ensure the selected room always appears in the list (even if it has an active rental) you can use ->options() instead of ->relationship(). This gives you full control over which rooms are shown, allowing you to include the selected room manually when editing a record.

Forms\Components\Select::make('room_id')
    ->label('Room')
    ->options(function ($state) {
        $query = \App\Models\Room::query()
            ->whereDoesntHave('rentals', fn ($q) =>
                $q->where('status', RentalStatus::ACTIVE)
            );

        // Always include the selected room (for edit pages)
        if ($state) {
            $query->orWhere('id', $state);
        }

        return $query->pluck('name', 'id');
    })
    ->searchable()
    ->preload()
    ->required()
    ->disabledOn('edit');

1 like
davidmarsmalow's avatar

@Rebwar You're absolutely corect man. Thanks a lot for your insight it really spots on where I didn't notice from the problem of my code.

Here is my updated code with the same concept and logic from yours. I just keep using relatioship() just so I get the relational auto-fetch from my database feature.

Forms\Components\Select::make('room_id')
	->relationship('room', 'id', modifyQueryUsing: function (Builder $query, $get) {
		$selectedId = $get('room_id');

		$query->where(function (Builder $q) use ($selectedId) {
			$q->whereDoesntHave('rentals', fn(Builder $q) => $q->where('status', RentalStatus::ACTIVE));

			if ($selectedId) {
				$q->orWhere('id', $selectedId);
			}
		});
	})
	->getOptionLabelFromRecordUsing(fn($record) => $record->name)
	->searchable()
	->preload()
	->required()
	->disabledOn('edit')
	->label('Room'),

Please or to participate in this conversation.