Raziul Islam
Raziul Islam
FFilament
Created by Raziul Islam on 9/1/2024 in #❓┊help
Adding demo mode in filament application
Demo mode means the create/update/delete actions won't be applicable. I used to do this via middleware in Laravel but wanted to do similar thing but can't figure it out. Is there any easy way to add demo mode in a filament panel?
12 replies
FFilament
Created by Raziul Islam on 5/27/2024 in #❓┊help
Show Table only on Search
I want to create a table widget where a field is searchable. The records will be empty by default and the result if searched.
3 replies
FFilament
Created by Raziul Islam on 4/17/2024 in #❓┊help
Nesting form fields depending on relationships
No description
10 replies
FFilament
Created by Raziul Islam on 3/28/2024 in #❓┊help
Allowed memory size of 134217728 bytes exhausted
No description
41 replies
FFilament
Created by Raziul Islam on 2/6/2024 in #❓┊help
Custom MoneyInput form field
I am using 'akaunting/laravel-money' package and custom cast that helps to store the value in cents in database and return money object while reading. ( A common approach for money) The only issue was with filament TextInput which doesn't understands the object so I had to make a custom field. This is the code:
<?php

namespace App\Forms\Components;

use Filament\Forms\Components\TextInput;
use Filament\Support\RawJs;

class MoneyInput extends TextInput
{
use Concerns\HasMoneyAttributes;

protected function setUp(): void
{
parent::setUp();

$this->inputMode('decimal')
->minValue(0)
->prefix(fn (MoneyInput $component) => $component->getCurrency()->getPrefix())
->suffix(fn (MoneyInput $component) => $component->getCurrency()->getSuffix())
->stripCharacters(fn (MoneyInput $component) => $component->getCurrency()->getDecimalMark())
->formatStateUsing(function (MoneyInput $component, $state) {
if (is_array($state) && array_key_exists('amount', $state)) {
return money($state['amount'], key($state['currency']))->valueWithoutZeroes();
}

return $state;
})
->dehydrateStateUsing(function (MoneyInput $component, $state) {
return str_replace($component->getCurrency()->getThousandsSeparator(), '', $state);
});
}

public function getMask(): string|RawJs|null
{
if (! $this->evaluate($this->moneyMask)) {
return null;
}

return RawJs::make(sprintf(
"\$money(\$input, '%s', '%s', %d);",
$this->getCurrency()->getDecimalMark(),
$this->getCurrency()->getThousandsSeparator(),
$this->getCurrency()->getPrecision()
));
}
}
<?php

namespace App\Forms\Components;

use Filament\Forms\Components\TextInput;
use Filament\Support\RawJs;

class MoneyInput extends TextInput
{
use Concerns\HasMoneyAttributes;

protected function setUp(): void
{
parent::setUp();

$this->inputMode('decimal')
->minValue(0)
->prefix(fn (MoneyInput $component) => $component->getCurrency()->getPrefix())
->suffix(fn (MoneyInput $component) => $component->getCurrency()->getSuffix())
->stripCharacters(fn (MoneyInput $component) => $component->getCurrency()->getDecimalMark())
->formatStateUsing(function (MoneyInput $component, $state) {
if (is_array($state) && array_key_exists('amount', $state)) {
return money($state['amount'], key($state['currency']))->valueWithoutZeroes();
}

return $state;
})
->dehydrateStateUsing(function (MoneyInput $component, $state) {
return str_replace($component->getCurrency()->getThousandsSeparator(), '', $state);
});
}

public function getMask(): string|RawJs|null
{
if (! $this->evaluate($this->moneyMask)) {
return null;
}

return RawJs::make(sprintf(
"\$money(\$input, '%s', '%s', %d);",
$this->getCurrency()->getDecimalMark(),
$this->getCurrency()->getThousandsSeparator(),
$this->getCurrency()->getPrecision()
));
}
}
and in the form:
MoneyInput::make('price')->required(),
MoneyInput::make('price')->required(),
Now it is working but I wanted to know if there is scope for improvement.
3 replies