#Problems using useForm() and writing to database.

6 messages · Page 1 of 1 (latest)

scenic plinth
#

Hello and thanks to everyone who is trying to help with this problem.

I'm using Laravel 10, Intertia + Vue3.
I have a View/Component called Create.vue, which has various input/form elements.
It's used to add new Employees to the database.
The problem: It seems to only take the first two inputs (name and surname) and tries to add them into the database, which doesn't work. It says it couldn't add it to the database because birthday has no default value specified - not needed since I will always provide it but it doesn't "recognize" that input.

My useForm():

const form = useForm({
    name: '',
    surname: '',
    birthday: '',
    address_street: '',
    address_number: '',
    address_postalcode: '',
    addresss_city: '',
    join_date: ''
});

My inputs fields/elements:

<input v-model="form.name" type="text" name="name" id="name" />
<input v-model="form.surname" type="text" name="surname" id="surname" />
<input v-model="form.birthday" type="date" name="birthday" id="birthday" />
<input v-model="form.address_street" type="text" name="address_street" id="address_street" />
<input v-model="form.address_number" type="text" name="address_number" id="address_number" />
<input v-model="form.address_postalcode" type="text" name="address_postalcode" id="address_postalcode" />
<input v-model="form.address_city" type="text" name="address_city" id="address_city" />

Additional information:

  • The request body is a JSON array containing ALL informations I have entered (everything seems to be correct here) even the birthday
  • The birthday column is a date type column. I tried to manually add a new entry to the table with the exact same values from the request body (for test-purposes) and it worked perfectly.
  • After filling out all input elements I get this error message with following SQL statement:
    Error message:
    SQLSTATE[HY000]: General error: 1364 Field 'birthday' doesn't have a default value

SQL statement from the error message page (other values are not considered as you can see):

insert into `employees` (`name`, `surname`, `updated_at`, `created_at`) values (TestName, ExampleSurname123, 2024-02-08 11:32:04, 2024-02-08 11:32:04)

Thank you for every kind of help! 🙂

#

Additionally the method in my controller to store all of this:

public function store(Request $request)
{
    $employee = new Employee($request->all());
    $employee->save();
    return redirect()->route('employees');
}
thorny blaze
#

Add birthday to the $fillable array on the model. Otherwise it gets ignored.

#

And you can save yourself some typing by using Employee::create() in stead of instantiating and saving it manually.

scenic plinth
#

Wow, I thank you very much my dear.