© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
FilamentF
Filament•3y ago•
10 replies
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
Filament banner
FilamentJoin
A powerful open source UI framework for Laravel • Build and ship admin panels & apps fast with Livewire
20,307Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

Mutate form data before create but for delete
FilamentFFilament / ❓┊help
3y ago
Mutate Data before Create (calculated column)
FilamentFFilament / ❓┊help
2y ago
Mutate Before Create Function
FilamentFFilament / ❓┊help
3y ago
Mutate ManageRelatedRecords data before saving
FilamentFFilament / ❓┊help
2y ago