1

I am using vuejs-datepicker and inertiajs, but when I try to update the database I am getting the error: Invalid datetime format: 1292 Incorrect date value: '2021-07-09T12:54:00.000Z' for column 'date_of_birth'

I tried formatting the date within the controller, but this doesn't save anything. What am I missing here?

eidt.vue:

<datepicker v-model="form.date_of_birth" name="Date of birth" />

data() {
      return {
        form: this.$inertia.form({
          _method: 'put',
          date_of_birth: this.application.date_of_birth,
        }),
      }
},

methods: {
      update() {
          this.form.put(this.route('applications.update', this.application.id))
      },
},

controller/applications.update:

User::find($application->user_id)->update(Request::only(
     ['date_of_birth' => date('Y-m-d H:i:s', $application->user->date_of_birth)],
));

2 Answers 2

1

Try using the following:

User::find($application->user_id)
    ->update([
        'date_of_birth' => Carbon::parse(request()->date_of_birth)->toDateTimeString()
    ]);

Depending on how the date is coming from the front end, you need to parse date_of_birth to a date time string which will be accepted by the database.

This can be done by using the Carbon::parse($value)->toDateTimeString() method above.

Make sure to import Carbon into the controller:

use Carbon\Carbon

Sign up to request clarification or add additional context in comments.

Comments

1

This is how we do it, for every datepicker date in the form we make a setter on the model e.g.

public function setDateOfBirthAttribute($value): void
{
    $this->attributes['date_of_birth'] = $value ? Carbon::parse($value) : null;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.