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
Arslan Fida
Arslan Fida2y ago
I think you should put this method in Create Page rather than Resource Page
HGalih
HGalihOP2y ago
It works... Amazing thanks! @tinkypinky but I have a follow up question. I am new to fillament
Arslan Fida
Arslan Fida2y ago
Sure
HGalih
HGalihOP2y ago
How am I supposed to know where to put this function? Is it written somewhere in the docs
No description
HGalih
HGalihOP2y ago
"Create Page Class" Okay nevermind..
Arslan Fida
Arslan Fida2y ago
Yes
HGalih
HGalihOP2y ago
Thank you again! I'm just rushing through the documentation. Rookie mistake

Did you find this page helpful?