Reputation: 395
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)],
));
Upvotes: 1
Views: 1184
Reputation: 15037
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;
}
Upvotes: 1
Reputation: 4202
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
Upvotes: 1