Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Surajshetty's avatar

Filament foreign key value not inserting in form

I am trying country, state and cities value also be insert when the user give all the person details but it not getting any value, below is the code for the form submit

public static function form(Form $form): Form
{
    return $form
        ->schema([
            Forms\Components\Section::make('User Details')
            ->schema([
                Forms\Components\TextInput::make('first_name')
                ->required()
                ->maxLength(255),
            Forms\Components\TextInput::make('last_name')
                ->required()
                ->maxLength(255),
            Forms\Components\TextInput::make('middle_name')
                ->required()
                ->maxLength(255),
            ])->columns(3),
            Forms\Components\Section::make('Address Details')
            ->schema([
            Forms\Components\Select::make('country_id')
                ->relationship(name: 'country', titleAttribute: 'name')
                ->searchable()
                ->preload()
                ->live()
                ->afterStateUpdated(function (Set $set) {
                    $set('state', null);
                    $set('cities', null);

                })
                ->multiple()
                ->required(),
            Forms\Components\Select::make('state_id')
                    ->relationship(name: 'States', titleAttribute: 'name')
                    ->options(fn (Get $get): array => State::query()
                    ->where('country_id', $get('country_id'))
                    ->pluck('name', 'id')
                    ->toArray()
                )
                ->searchable()
                ->preload()
                ->live()
                ->afterStateUpdated(function (Set $set) {
                    $set('city_id', null);
                })
                ->multiple()
                ->required(),
            Forms\Components\Select::make('city_id')
                    ->relationship(name: 'City', titleAttribute: 'name')
                    ->options(fn (Get $get): array => City::query()
                    ->where('state_id', $get('state_id'))
                    ->pluck('name', 'id')
                    ->toArray()
                )
                ->searchable()
                ->preload()
                ->live()
                ->multiple()
                ->required(),
            Forms\Components\Select::make('department_id')
                ->relationship(name: 'department', titleAttribute: 'name')
                ->searchable()
                ->preload()
                ->multiple()
                ->required(),
                Forms\Components\TextInput::make('zip_code')
                    ->required()
                    ->maxLength(255),
                Forms\Components\TextInput::make('address')
                    ->required()
                    ->columnSpanFull()
                    ->maxLength(255),
            ])->columns(3),
            Forms\Components\Section::make('Date')
            ->schema([
                Forms\Components\DatePicker::make('date_of_birth')
                    ->displayFormat('d/m/Y')
                    ->required(),
                Forms\Components\DatePicker::make('date_hired')
                    ->displayFormat('d/m/Y')
                    ->required(),
            ])->columns(3),
            // Forms\Components\Hidden::make('country_id'),
            // Forms\Components\Hidden::make('state_id'),
            // Forms\Components\Hidden::make('city_id')
        ])->columns(3);
}
0 likes
1 reply
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

It seems like you're trying to create a form with Filament that includes dependent dropdowns for country, state, and city, and you want to ensure that the selected values for these fields are properly inserted into the database when the form is submitted.

From the code you've provided, it looks like you're using the relationship method to create the dropdowns, but you're also using the multiple() method, which is intended for selecting multiple values. If you're trying to insert a single foreign key value for each of these fields, you should not use multiple().

Here's a revised version of your form schema without the multiple() method and with the correct relationship names (assuming your relationships are named country, state, and city in your model):

public static function form(Form $form): Form
{
    return $form
        ->schema([
            Forms\Components\Section::make('User Details')
                ->schema([
                    Forms\Components\TextInput::make('first_name')
                        ->required()
                        ->maxLength(255),
                    Forms\Components\TextInput::make('last_name')
                        ->required()
                        ->maxLength(255),
                    Forms\Components\TextInput::make('middle_name')
                        ->required()
                        ->maxLength(255),
                ])->columns(3),
            Forms\Components\Section::make('Address Details')
                ->schema([
                    Forms\Components\Select::make('country_id')
                        ->relationship('country', 'name') // Assuming the relationship name is 'country'
                        ->searchable()
                        ->preload()
                        ->afterStateUpdated(function (Set $set) {
                            $set('state_id', null);
                            $set('city_id', null);
                        })
                        ->required(),
                    Forms\Components\Select::make('state_id')
                        ->relationship('state', 'name') // Assuming the relationship name is 'state'
                        ->options(fn (Get $get): array => State::query()
                            ->where('country_id', $get('country_id'))
                            ->pluck('name', 'id')
                            ->toArray()
                        )
                        ->searchable()
                        ->preload()
                        ->afterStateUpdated(function (Set $set) {
                            $set('city_id', null);
                        })
                        ->required(),
                    Forms\Components\Select::make('city_id')
                        ->relationship('city', 'name') // Assuming the relationship name is 'city'
                        ->options(fn (Get $get): array => City::query()
                            ->where('state_id', $get('state_id'))
                            ->pluck('name', 'id')
                            ->toArray()
                        )
                        ->searchable()
                        ->preload()
                        ->required(),
                    Forms\Components\Select::make('department_id')
                        ->relationship('department', 'name')
                        ->searchable()
                        ->preload()
                        ->required(),
                    Forms\Components\TextInput::make('zip_code')
                        ->required()
                        ->maxLength(255),
                    Forms\Components\TextInput::make('address')
                        ->required()
                        ->columnSpanFull()
                        ->maxLength(255),
                ])->columns(3),
            Forms\Components\Section::make('Date')
                ->schema([
                    Forms\Components\DatePicker::make('date_of_birth')
                        ->displayFormat('d/m/Y')
                        ->required(),
                    Forms\Components\DatePicker::make('date_hired')
                        ->displayFormat('d/m/Y')
                        ->required(),
                ])->columns(3),
        ])->columns(3);
}

Make sure that the relationship method's first argument matches the name of the relationship method defined in your Eloquent model. Also, ensure that your model has the appropriate relationships defined for country, state, and city.

If you're still facing issues with inserting the foreign key values, please check your model's fillable or guarded properties to ensure that the foreign key columns (country_id, state_id, city_id) are fillable. Additionally, verify that your database table structure allows for these foreign key columns to be inserted and that there are no database-level constraints preventing the insertion.

1 like

Please or to participate in this conversation.