ariefng/filament-calculator
A Filament plugin that adds a simple calculator tool to your admin panel. Useful for quick calculations while managing data, with easy installation and seamless integration into Filament pages and resources.
Installation
composer require ariefng/filament-calculator
Publish the package assets (if needed):
php artisan vendor:publish --provider="Ariefng\FilamentCalculator\FilamentCalculatorServiceProvider" --tag="filament-calculator-assets"
Register the Action
Add the CalculatorAction to a Filament Table or Form field:
use Ariefng\FilamentCalculator\Actions\CalculatorAction;
CalculatorAction::make()
->label('Open Calculator')
->modalHeading('Enter Calculation')
->modalWidth('50%')
->expression('(value * 2) + 10') // Default expression (optional)
->successfulAction(function (string $result) {
return action('edit')->withResult($result);
}),
Attach to a Field
Apply the action to a TextInput field in a Table column or Form field:
TextInput::make('price')
->columnSpanFull()
->actions([
CalculatorAction::make(),
]),
Table displaying product prices where users need to quickly calculate adjusted values (e.g., discounts, taxes).use Filament\Tables\Table;
use Ariefng\FilamentCalculator\Actions\CalculatorAction;
Table::make(Product::class, [
'columns' => [
TextInput::make('price')
->actions([
CalculatorAction::make()
->expression('value * 0.9') // Apply 10% discount
->successfulAction(fn ($result) => action('edit')->withResult($result)),
]),
],
]);
(value * 0.8) + 5) and submit to update the field.Dynamic Expressions
Use variables like value (current field value) or taxRate (passed via extraVariables):
CalculatorAction::make()
->expression('(value + taxRate) * quantity')
->extraVariables([
'taxRate' => 0.08,
'quantity' => 2,
]),
Form Integration
Attach to a Form field for inline calculations:
TextInput::make('total')
->extraAttributes(['readonly' => true])
->actions([
CalculatorAction::make()
->expression('basePrice + shipping')
->successfulAction(fn ($result) => $this->form->fill(['total' => $result])),
]),
Conditional Logic Disable the calculator based on field state:
TextInput::make('discountedPrice')
->visible(fn ($record) => $record->is_discounted)
->actions([
CalculatorAction::make()
->visible(fn ($record) => $record->is_discounted),
]),
resources/lang/).CalculatorAction::make()
->modalWidth('60%')
->modalHeight('400px'),
$this->form->fill([
'calculated_value' => $this->validateExpression($result),
]);
private function validateExpression(string $expression): float {
// Implement logic to ensure $expression is safe/evaluable.
}
Expression Injection Risks
system('rm -rf /')).// Example: Only allow basic arithmetic.
$allowedFunctions = ['+', '-', '*', '/', 'abs'];
if (!preg_match("/^[0-9+\-*/().\s]+$/", $expression)) {
throw ValidationException::withMessages(['expression' => 'Invalid characters.']);
}
Modal Overlap
modalWidth or use z-index in custom CSS:
CalculatorAction::make()
->extraAttributes(['class' => 'filament-calculator--custom']),
.filament-calculator--custom .modal-content {
z-index: 9999;
}
Field Type Mismatch
TextInput::make('price')
->numeric()
->rules(['numeric']),
F12) to inspect modal initialization errors.npm run dev # If using Vite
php artisan view:clear
value + 1) to isolate issues.Custom Modal Content Override the default modal template by publishing and modifying:
php artisan vendor:publish --tag="filament-calculator-views"
Edit resources/views/vendor/filament-calculator/modal.blade.php.
Add Buttons Extend the modal with additional buttons (e.g., "Save & Close"):
CalculatorAction::make()
->extraModalButtons([
Button::make('Save')
->action('saveAndClose'),
]),
Hooks for Pre/Post Calculation
Use Filament’s beforeFill or afterFill hooks to modify behavior:
$this->form->fill([
'calculated_field' => $result,
])->afterFill(function () {
// Trigger additional logic (e.g., update related records).
});
How can I help you explore Laravel packages today?