© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
FilamentF
Filament•3y ago•
4 replies
barrerakj

Same exact code on different Fields

Hey, easy question here. I have 3 different fields that have the same exact code in the ->afterStateUpdated(). I always try to follow good practices and not to repeat code, but I'm not sure how to do this. This is how it looks

->afterStateUpdated(function (callable $set, $get) {
$quantity = $get('quantity') == '' ? 1 : $get('quantity');
$width = $get('width') == '' ? 1 : $get('width');
$height = $get('height') == '' ? 1 : $get('height');
if($get('height') == '' or $get('width') == ''){
$set('totalm', 0);
} else {
$set('totalm', bcdiv(((($width * $height) * $quantity)),1,2) );
}
})

And it's exactly the same in 3 different fields. It actually doesn't matter what's inside or the logic around it, I just want to ask if I can write the code just once somewhere and use it where I want. I'm actually not sure because of the $set and $get callables.
Filament banner
FilamentJoin
A powerful open source UI framework for Laravel • Build and ship admin panels & apps fast with Livewire
20,307Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

same fields, different content
FilamentFFilament / ❓┊help
3y ago
Fields with same name within different section fields
FilamentFFilament / ❓┊help
3y ago
Form relation fields of same relation in different groups/Fieldset
FilamentFFilament / ❓┊help
3y ago
Same repeater , different extra actions
FilamentFFilament / ❓┊help
2y ago