F
Filament10mo ago
HGalih

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
Solution:
I think you should put this method in Create Page rather than Resource Page
Jump to solution
7 Replies
Solution
tinkypinky
tinkypinky10mo ago
I think you should put this method in Create Page rather than Resource Page
HGalih
HGalihOP10mo ago
It works... Amazing thanks! @tinkypinky but I have a follow up question. I am new to fillament
tinkypinky
tinkypinky10mo ago
Sure
HGalih
HGalihOP10mo ago
How am I supposed to know where to put this function? Is it written somewhere in the docs
No description
HGalih
HGalihOP10mo ago
"Create Page Class" Okay nevermind..
tinkypinky
tinkypinky10mo ago
Yes
HGalih
HGalihOP10mo ago
Thank you again! I'm just rushing through the documentation. Rookie mistake
Want results from more Discord servers?
Add your server