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;
    }


Return error

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
Was this page helpful?