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?
No description
15 Replies
Dennis Koch
Dennis Koch2mo ago
It's hard to read the code, when it's not properly formatted in Discord
matin rajabi
matin rajabi2mo ago
hi yes you're right
No description
Dennis Koch
Dennis Koch2mo ago
Is the VarietyDetail you selected still part of options on the edit page?
matin rajabi
matin rajabi2mo ago
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
No description
Dennis Koch
Dennis Koch2mo ago
When you load the page, what's the value of $get('product_id') and which VarietyDetails are loaded?
matin rajabi
matin rajabi2mo ago
i figured out that i send wrong image this is the code i have problem in
No description
matin rajabi
matin rajabi2mo ago
it works in create page. but in edit page no
toeknee
toeknee2mo ago
That's a bit of a mess. You should use a relationship for the data, and use getOptionLabelUsing for the label.
Select::make('product_id')
->relationship('product_options', 'title')
Select::make('product_id')
->relationship('product_options', 'title')
public function myRelationshipProducts() {
return $this->hasMany(Poduct::class)->where('user_id', auth()->user()->id);
}
public function myRelationshipProducts() {
return $this->hasMany(Poduct::class)->where('user_id', auth()->user()->id);
}
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.
Dennis Koch
Dennis Koch2mo ago
Btw. why is $product an array? That should be a model
toeknee
toeknee2mo ago
True that!
matin rajabi
matin rajabi2mo ago
i should check a condition which can't access the column with one relationship so i couldn't use relationship
toeknee
toeknee2mo ago
Don’t check the condition, but add a where onto the relationship?
matin rajabi
matin rajabi2mo ago
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
toeknee
toeknee2mo ago
You would add the relationship then follow the documentation: https://filamentphp.com/docs/3.x/forms/fields/select#customizing-the-relationship-query
Select::make('product_id')
->relationship(
name: 'product_options',
titleAttribute: 'title',
modifyQueryUsing: fn (Builder $query) => $query->with('categories')->whereHas('categories.variety_id'),
)
->getOptionLabelFromRecordUsing(fn ($record) => "{$record->title} ({$record->myRelation->count()})")
Select::make('product_id')
->relationship(
name: 'product_options',
titleAttribute: 'title',
modifyQueryUsing: fn (Builder $query) => $query->with('categories')->whereHas('categories.variety_id'),
)
->getOptionLabelFromRecordUsing(fn ($record) => "{$record->title} ({$record->myRelation->count()})")
or similar depending on your code
matin rajabi
matin rajabi2mo ago
thanks a lot man.