select field display id of record in edit page
hello guys i have a form with a select option field like this:
Select::make('variety_detail_id')
->options(
function (callable $get) {
if ($get('product_id')) {
$category_id = Product::where(['id' => $get('product_id')])->get()[0]['category_id'];
$variety_id = Category::where('id', $category_id)->get()[0]['variety_id'];
return VarietyDetail::where('variety_id', $variety_id)->get()||->pluck('value', 'id')||;
}
}
)
i set a label for each option but in edit page it doesn't have any label, so display its id like in the image
what should i do for this?Solution:Jump to solution
You would add the relationship then follow the documentation:
https://filamentphp.com/docs/3.x/forms/fields/select#customizing-the-relationship-query
```php...
15 Replies
It's hard to read the code, when it's not properly formatted in Discord
hi yes you're right
Is the VarietyDetail you selected still part of options on the edit page?
yes and it shows up with the id, but it should display label
in the image, 21 is the id of VarietyDetail but it should be value of another column in table where id=21
When you load the page, what's the value of
$get('product_id')
and which VarietyDetails are loaded?i figured out that i send wrong image
this is the code i have problem in
it works in create page. but in edit page no
That's a bit of a mess. You should use a relationship for the data, and use getOptionLabelUsing for the label.
I suspect your issue you have is actually you are not filling the data properly in the edit becuase you are filling it above with the query. The relationship should solve that.
Btw. why is
$product
an array? That should be a modelTrue that!
i should check a condition which can't access the column with one relationship
so i couldn't use relationship
Don’t check the condition, but add a where onto the relationship?
lets expand it a bit. i have a PRODUCTS table which have relation with CATEGORIES and PRODUCT_VARIETY. i should dump some rows from options. the ones that ->CATEGORY->variety_id == null and ->PRODUCT_VARIETY->count() > 0.
this is the situation and unfortunately i don't know how to say this with relation method of select in filament.
Solution
You would add the relationship then follow the documentation:
https://filamentphp.com/docs/3.x/forms/fields/select#customizing-the-relationship-query
or similar depending on your code
thanks a lot man.