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

rayzor's avatar

Filament form is working, but SQL is not putting all the fields in.

I'm running through a tutorial to learn Filament and the form works and shows all of the pulldowns based on relationships. The form in the Resource is:

    public static function form(Form $form): Form
    {
        return $form
            ->schema
            ([
                Card::make()
                ->schema([
                    Select::make('country_id')->relationship('country', 'name')->required(),
                    Select::make('state_id')->relationship('state', 'name')->required(),
                    Select::make('city_id')->relationship('city', 'name')->required(),
                    Select::make('department_id')->relationship('department', 'name')->required(),
                    TextInput::make('first_name')->required(),
                    TextInput::make('last_name')->required(),
                    TextInput::make('address')->required(),
                    TextInput::make('zip_code')->required(),
                    DatePicker::make('birth_date')->required(),
                    DatePicker::make('date_hired')->required()
                ])
            ]);
    }

And I'm getting this MySQL Error:

SQLSTATE[HY000]: General error: 1364 Field 'state_id' doesn't have a default value (Connection: mysql, SQL: insert into `employees` (`country_id`, `department_id`, `first_name`, `last_name`, `address`, `zip_code`, `birth_date`, `date_hired`, `updated_at`, `created_at`) values (2, 1, Test, test, 1234 test, 12345, 2020-04-03, 2023-04-01, 2023-04-13 00:50:54, 2023-04-13 00:50:54))

I see that "state_id" is not anywhere in the query and noticed city_id is also missing. Additional information, I have built the state resources and can input a new state. I'm not sure why the field state_id is missing when Country, Department

0 likes
3 replies
LaryAI's avatar
Level 58

The error message suggests that the "state_id" field is not being included in the SQL insert statement. This could be due to a few reasons:

  1. The "state_id" field is not marked as fillable in the Employee model. Make sure that the "state_id" field is included in the $fillable array in the Employee model.

  2. The "state_id" field is not being set in the form submission. Check that the "state_id" field is being set in the form submission and that it is not being overwritten by any default values.

  3. There may be a database constraint that is preventing the "state_id" field from being set. Check that there are no database constraints that are preventing the "state_id" field from being set.

Here's an example of how to include the "state_id" field in the form:

public static function form(Form $form): Form
{
    return $form
        ->schema([
            Card::make()
                ->schema([
                    Select::make('country_id')->relationship('country', 'name')->required(),
                    Select::make('state_id')->relationship('state', 'name')->required(),
                    Select::make('city_id')->relationship('city', 'name')->required(),
                    Select::make('department_id')->relationship('department', 'name')->required(),
                    TextInput::make('first_name')->required(),
                    TextInput::make('last_name')->required(),
                    TextInput::make('address')->required(),
                    TextInput::make('zip_code')->required(),
                    DatePicker::make('birth_date')->required(),
                    DatePicker::make('date_hired')->required(),
                    Hidden::make('state_id')->value(request()->input('state_id')),
                    Hidden::make('city_id')->value(request()->input('city_id')),
                ]),
        ]);
}

This includes two hidden fields for "state_id" and "city_id" that are populated with the values from the form submission.

rayzor's avatar

Thanks AI for the try. Ichecked and did include the fields in filament correctly and verified the fields are fillable. Here is the stuff from the employee model:

class Employee extends Model
{
    use HasFactory;

    protected $fillable =['first_name', 
    'last_name',
    'address',
    'city_id'. 
    'state_id', 
    'country_id',
    'department_id', 
    'zip_code',
    'birth_date',
    'date_hired'];

    public function country()
    {
        return $this->belongsTo(Country::class);
    
    }

    public function state()
    {
        return $this->belongsTo(State::class);
    }

    public function city()
    {
        return $this->belongsTo(City::class);
    }

    public function department()
    {
        return $this->belongsTo(Department::class);
    }
rayzor's avatar
rayzor
OP
Best Answer
Level 1

I figured it out, looking at the model, I had a typo of a period '.' after city_id in the fillable fields.

Please or to participate in this conversation.