F
Filament4mo ago
nowak

Using nested relationship as option labels / titleAttribute for Select field

I have a relationship Select field in my form that stores product_sku_id which is the id for my ProductSku model. My ProductSku model does not store attribute values, like Small or Large, because those are stored in the AttributeValue model, which ProductSku has a ManyToMany relationship with. So I can easily use the sku attribute as the titleAttribute for the option labels, as it is an attribute on the ProductSku model. But I want to use ProductSku::attributeValues->value as the titleAttribute instead. Is there a way to achieve this by using eloquent relationships? Or am I forced to store the attribute values on the product_skus table directly to make this work? Here is my current product_sku_id Select field with a hopeful attempt to get this to work, but unsuccessfully:
Select::make('product_sku_id')
->label('SKU')
->relationship('productSku', 'attributeValues.value')
->live()
->afterStateUpdated(function ($state, Set $set, Get $get) {
\Log::info('sku state', ['state' => $state]);
$set ('price', ProductSku::find($state)?->price ?? 0);
})
->required(),
Select::make('product_sku_id')
->label('SKU')
->relationship('productSku', 'attributeValues.value')
->live()
->afterStateUpdated(function ($state, Set $set, Get $get) {
\Log::info('sku state', ['state' => $state]);
$set ('price', ProductSku::find($state)?->price ?? 0);
})
->required(),
I have seen people using something like this to save data to relationships:
Group::make()
->relationship('metadata')
->schema([
Select::make()
Group::make()
->relationship('metadata')
->schema([
Select::make()
But I am not sure if and how this could be used for my use case, as I still need my select field to store the product_sku_id`. Any help is highly appreciated!
0 Replies
No replies yetBe the first to reply to this messageJoin