HGalih
HGalih
FFilament
Created by HGalih on 2/4/2024 in #❓┊help
Conditional Resource/Navigation Label
No description
5 replies
FFilament
Created by HGalih on 2/4/2024 in #❓┊help
get new repeater item only filament when editing
I want to decrease user balance everytime they order. This is what I did
protected function mutateFormDataBeforeCreate(array $data): array
{
$user = Auth::user();
$data['user_id'] = $user->id;
if(auth()->user()->balance < $data['total_price'])
throw new \Exception('Insufficient balance');

foreach ($this->data['paket_bot'] as &$value) {
$package = \App\Models\BotPackage::find($value['bot_package_id']);
$value['expired_at'] = now()->addDays($package->day_count);
if($value['quantity'] < 1) $value['quantity'] = 1;
$value['price'] = $package->discounted * $value['quantity'];
}

$user->decreaseOrIncreaseBalance(-$data['total_price']);
return $data;
}
protected function mutateFormDataBeforeCreate(array $data): array
{
$user = Auth::user();
$data['user_id'] = $user->id;
if(auth()->user()->balance < $data['total_price'])
throw new \Exception('Insufficient balance');

foreach ($this->data['paket_bot'] as &$value) {
$package = \App\Models\BotPackage::find($value['bot_package_id']);
$value['expired_at'] = now()->addDays($package->day_count);
if($value['quantity'] < 1) $value['quantity'] = 1;
$value['price'] = $package->discounted * $value['quantity'];
}

$user->decreaseOrIncreaseBalance(-$data['total_price']);
return $data;
}
'paket_bot' is a repeater that hold product datas. The catch is they can also update it (add more product but not delete previous product). And if they do add another product, I need to decrease the balance again. How do I avoid decreasing the user balance for the previous ordered item? (because repeater hold both old and new product data)
2 replies
FFilament
Created by HGalih on 2/3/2024 in #❓┊help
Very simple mutate data before create. But it is not working
It must be a very stupid mistake. This is a fresh filament and I just want to implement a very simple feature that auto assign role column for user
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('name')
->required()
->maxLength(255),
Forms\Components\TextInput::make('email')
->email()
->required()
->maxLength(255),
Forms\Components\TextInput::make('password')
->password()
->dehydrateStateUsing(fn ($state) => Hash::make($state))
->dehydrated(fn ($state) => filled($state))
->required(fn (string $context): bool => $context === 'create'),
Forms\Components\TextInput::make('balance')
->label('Starting Balance')
->required()
->numeric()
->hidden(fn (string $context): bool => $context !== 'create')
->default(0),
Forms\Components\TextInput::make('percentage_discount')
->required()
->numeric()
->default(0),
Forms\Components\TextInput::make('fixed_discount')
->required()
->numeric()
->default(0),

]);


}

protected function mutateFormDataBeforeCreate(array $data): array
{
$data['role'] = 1;

return $data;
}
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('name')
->required()
->maxLength(255),
Forms\Components\TextInput::make('email')
->email()
->required()
->maxLength(255),
Forms\Components\TextInput::make('password')
->password()
->dehydrateStateUsing(fn ($state) => Hash::make($state))
->dehydrated(fn ($state) => filled($state))
->required(fn (string $context): bool => $context === 'create'),
Forms\Components\TextInput::make('balance')
->label('Starting Balance')
->required()
->numeric()
->hidden(fn (string $context): bool => $context !== 'create')
->default(0),
Forms\Components\TextInput::make('percentage_discount')
->required()
->numeric()
->default(0),
Forms\Components\TextInput::make('fixed_discount')
->required()
->numeric()
->default(0),

]);


}

protected function mutateFormDataBeforeCreate(array $data): array
{
$data['role'] = 1;

return $data;
}
Return error
SQLSTATE[HY000]: General error: 1364 Field 'role' doesn't have a default value
SQLSTATE[HY000]: General error: 1364 Field 'role' doesn't have a default value
And ofcourse I already defined 'role' ass fillable on my user.php
11 replies
FFilament
Created by HGalih on 12/6/2023 in #❓┊help
--generate to use Select Field instead of Text Input for foreign key?
Anyway to make --generate command to use select column with relationship instead of text input where we need to fill in id for foreign keys column
4 replies
FFilament
Created by HGalih on 11/28/2023 in #❓┊help
Fillament store asset url referencing wrong domain.
Hi for some reason fillament reference the wrong domain. I duplicated my laravel project and put it in anothe rdomain. When displaying an image, it instead reference to my old domain. I have make sure that .env file is setup up properly with my new domain. Anything else needed?
2 replies
FFilament
Created by HGalih on 11/27/2023 in #❓┊help
Filament Reactive not Working
My repeater component visibility and requiribility is dependant of my select component. What I do: 1. Make both element reactive 2. Use Callable $get What I expect: 1. It start with the repeater showing because default value is 1 2. As soon as I change my select value, repeater become hidden because value is no longer 1 3. As soon as I put it back to 1, the repeater pops up back Problem: 1. Works perfectly 2. Works Perfectly 3. Doesn't want to show it anymore at all Code:
public static function form(Form $form): Form
{
return $form
->columns(3)
->schema([
Forms\Components\Select::make('exercise_type_id')->label("Question Type")
->relationship('exerciseType', 'name')
->reactive()
->default(1)
->required(),
Forms\Components\RichEditor::make("question")->columnSpan(3)->required(),
Forms\Components\RichEditor::make("explanation")->columnSpan(3),
Repeater::make('answerlist')
->defaultItems(4)
->relationship('multipleChoiceAnswers')
->label("Multiple Choice Answer Option")
->grid(2)
->schema([
Forms\Components\TextInput::make('text')->label("Text Answer"),
Forms\Components\FileUpload::make('img')->label("Image Answer")
->directory('module-images')
->storeFileNamesIn("original_filename"),
Forms\Components\Checkbox::make('is_correct_option')->label("Mark Answer As Correct")->default(false),
])->columnSpan(3)->required(fn(Callable $get) => ($get('exercise_type_id') == 1))->hidden(fn(Callable $get) => ($get('exercise_type_id') !== 1))->reactive(),
]);
}
public static function form(Form $form): Form
{
return $form
->columns(3)
->schema([
Forms\Components\Select::make('exercise_type_id')->label("Question Type")
->relationship('exerciseType', 'name')
->reactive()
->default(1)
->required(),
Forms\Components\RichEditor::make("question")->columnSpan(3)->required(),
Forms\Components\RichEditor::make("explanation")->columnSpan(3),
Repeater::make('answerlist')
->defaultItems(4)
->relationship('multipleChoiceAnswers')
->label("Multiple Choice Answer Option")
->grid(2)
->schema([
Forms\Components\TextInput::make('text')->label("Text Answer"),
Forms\Components\FileUpload::make('img')->label("Image Answer")
->directory('module-images')
->storeFileNamesIn("original_filename"),
Forms\Components\Checkbox::make('is_correct_option')->label("Mark Answer As Correct")->default(false),
])->columnSpan(3)->required(fn(Callable $get) => ($get('exercise_type_id') == 1))->hidden(fn(Callable $get) => ($get('exercise_type_id') !== 1))->reactive(),
]);
}
3 replies