F
Filamentβ€’2y ago
Augus

Is this a bug or intended behaviour? (Select relationship)

So i got the following select field, do note that 'full_name' is an virtual column:
Forms\Components\Select::make('ram_id')
->label('Ram')
->required()
->relationship(
'ram',
'full_name',
fn(Builder $query) => $query
->where('active', '=', true)
->where('gender', '=', 'M')
)
->preload()
->searchable(),
Forms\Components\Select::make('ram_id')
->label('Ram')
->required()
->relationship(
'ram',
'full_name',
fn(Builder $query) => $query
->where('active', '=', true)
->where('gender', '=', 'M')
)
->preload()
->searchable(),
Just to be sure the virtual column:
$table->string('full_name')->storedAs("country_code || registration_number || ' - ' || name");
$table->string('full_name')->storedAs("country_code || registration_number || ' - ' || name");
But when i got a Resource where the already select Ram is not active anymore it doesn't show the 'full_name' but the ID of that Ram. I would suspect that it would still show the 'full_name' cause i see no reason why not. I got it fixed myself though by adding the following, but again i think it shouldn't have to:
->getOptionLabelUsing(fn ($value): ?string => Animal::find($value)?->full_name)
->getOptionLabelUsing(fn ($value): ?string => Animal::find($value)?->full_name)
This shows the 'full_name' even though the Ram is not active anymore.
14 Replies
Dennis Koch
Dennis Kochβ€’2y ago
Use Select::make('ram.full_name')
Dan Harrin
Dan Harrinβ€’2y ago
i dont think so, thats table syntax its weird feels like something is wrong with the storedAs can you try simple concatenation instead of the ||
Dennis Koch
Dennis Kochβ€’2y ago
That is concatenation
Dan Harrin
Dan Harrinβ€’2y ago
ive seen $table->string('full_name')->virtualAs('concat(first_name, \' \', last_name)'); just something simple like that
Dennis Koch
Dennis Kochβ€’2y ago
It’s Postgres syntax I think
Dan Harrin
Dan Harrinβ€’2y ago
oh, ew
Augus
AugusOPβ€’2y ago
Yea it's Postgres Im making a big data platform, so Postgress seemed smarter
Dennis Koch
Dennis Kochβ€’2y ago
Postgres always seems smarter πŸ˜…
Augus
AugusOPβ€’2y ago
Yea still not sure if it's the right way, first time i use Postgres. And since im still in the development fase i can't really see the difference So i hope that in the end it's the right choice This doesn't solve it btw
cheesegrits
cheesegritsβ€’2y ago
Out of interest, is 'active' false/0 or null on the problematic rows?
Augus
AugusOPβ€’2y ago
false/true
toeknee
toekneeβ€’2y ago
Did you end up solving it?
The Milkman
The Milkmanβ€’2y ago
@busybeard ?
Augus
AugusOPβ€’2y ago
@milkmannetje thanks for the mention πŸ™‚ @toeknee_iom & @milkmannetje this is what i end up with
Forms\Components\Select::make('ram_id')
->label('Ram')
->required()
->relationship(
'ram',
'full_name',
fn(Builder $query) => $query
->where('active', '=', true)
->where('gender', '=', 'M')
)
->getOptionLabelUsing(fn ($value): ?string => Animal::find($value)?->full_name)
->preload()
->searchable(),
Forms\Components\Select::make('ram_id')
->label('Ram')
->required()
->relationship(
'ram',
'full_name',
fn(Builder $query) => $query
->where('active', '=', true)
->where('gender', '=', 'M')
)
->getOptionLabelUsing(fn ($value): ?string => Animal::find($value)?->full_name)
->preload()
->searchable(),
Deleted the first Wherehas, thats some code im still working on πŸ™‚
Want results from more Discord servers?
Add your server