F
Filament9mo ago
nowak

Show name instead of id in Select relationship field, upon record soft deletion.

When I have a Select field like so:
Select::make('product_id')
->label('Product')
->relationship('product', 'name', function (Builder $query, Get $get) {
$mealTypeId = $get('../../meal_type_id');
$query->byMealType($mealTypeId);
})
->preload()
->required()
->distinct()
->disableOptionsWhenSelectedInSiblingRepeaterItems()
->columnSpan([
'md' => 5,
])
->searchable(),
Select::make('product_id')
->label('Product')
->relationship('product', 'name', function (Builder $query, Get $get) {
$mealTypeId = $get('../../meal_type_id');
$query->byMealType($mealTypeId);
})
->preload()
->required()
->distinct()
->disableOptionsWhenSelectedInSiblingRepeaterItems()
->columnSpan([
'md' => 5,
])
->searchable(),
My field shows the group names in the selection options, and when a resource record is created, the product name is also shown in the Select field in the edit form. See first screenshot. If I delete the product that was selected, the Select field in the edit form shows the id of the previously existing product instead of the it's name. See second screenshot. I understand that the non-exising product should not be selectable after it is deleted, but I would love to be able to show the product name instead of the id, when the product has been deleted. Is there a way to force Filament Select fields to use name instead of id in relationships, or do something else that achieves what I want to do?
No description
No description
Solution:
I think what I want to implement is not possible, as the field just shows what is saved in the pivot table record in the database, which is the product_id. So to avoid showing product id's in case the products have been removed, I added a static product_name field to my pivot table, and added this to my filament form, which gets set in mutateRelationshipDataBeforeCreateUsing(). Then I show the Select relationship product_id field or the product_name conditionally, based on the $record...
Jump to solution
7 Replies
LeandroFerreira
LeandroFerreira9mo ago
$query ... ->withTrashed() ?
H4L1M
H4L1M9mo ago
hi; out of ur question ; whats $get('../../meal_type_id') ??? can u explain it pls
nowak
nowakOP9mo ago
This doesn't block soft deleted records from being selectable, so this doesn't solve it.
Solution
nowak
nowak9mo ago
I think what I want to implement is not possible, as the field just shows what is saved in the pivot table record in the database, which is the product_id. So to avoid showing product id's in case the products have been removed, I added a static product_name field to my pivot table, and added this to my filament form, which gets set in mutateRelationshipDataBeforeCreateUsing(). Then I show the Select relationship product_id field or the product_name conditionally, based on the $record and the status_id. This works for me, as the order items product fields should not be editable after an order deadline has passed, so the order items become disabled informative fields in the edit form.
nowak
nowakOP9mo ago
My final function/solution is here:
Want results from more Discord servers?
Add your server