F
Filamentβ€’13mo ago
astronomic

How can I create a virtual column with a name field from another table

I need to create a virtual column (or something similar) that concatenates the name of the product (products table) with the color of the product variant (product_variants table). The virtual column would be in the variant table. Is there any way to do it?
8 Replies
astronomic
astronomicβ€’13mo ago
How can I do it with a Laravel 9 accesor? I try but it gives me an error. this is my code Forms\Components\Select::make('product_variant_id') ->label('Producto') ->required() ->searchable() ->options(ProductVariant::take(20)->pluck('full_name_product', 'id')) ->required() ->columnSpan(3),
Husky110
Husky110β€’13mo ago
I'm kind of doing the same thing. I think your options()-call is wrong. Try this:
->options(fn() => ProductVariant::take(20)->pluck('full_name_product', 'id'))
->options(fn() => ProductVariant::take(20)->pluck('full_name_product', 'id'))
astronomic
astronomicβ€’13mo ago
Thank you! but it always worked for me like this. I have discovered that without the get(), it does not work for me, now it does: ->options(ProductVariant::get()->pluck('full_name', 'id'))
Husky110
Husky110β€’13mo ago
That's cause ->get gives you a Collection, ->pluck on a Collection gives you an array. ->options expects either an array or a closure.
->options(fn() => ProductVariant::take(20)->pluck('full_name_product', 'id'))
->options(fn() => ProductVariant::take(20)->pluck('full_name_product', 'id'))
is basically the same as
->options(
function () {
return ProductVariant::take(20)->pluck('full_name_product', 'id');
}
)
->options(
function () {
return ProductVariant::take(20)->pluck('full_name_product', 'id');
}
)
astronomic
astronomicβ€’13mo ago
Ahh ok, I undertand, thank you so much!!
Husky110
Husky110β€’13mo ago
Try playing arround with some of those anonymous functions. Filament can do some pretty funky stuff with those. πŸ™‚ See where you can pass a Closure and go for it.
astronomic
astronomicβ€’13mo ago
hahaha, I'll listen to you, thank you very much once again 😊
Husky110
Husky110β€’13mo ago
be my guest. πŸ™‚ I'm just startin to get the ropes of filament myself tho. πŸ˜„