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

freemium's avatar

insert year-month in db Incorrect date value: '2021-08'

how to insert in database year-month only i am getting error as

SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect date value: '2020-08'

my migration file

$table->date('year_month');

my html form blade

<input name="year_month" type="month" >

controller

Model_name::create($request->all());

is there any mistake in migration file or controller help to resolve this issue

0 likes
4 replies
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

a date is year-month-day... You cant just leave the day out..

Either save it correctly (perhaps just always the 1 of the month), or use a string column

Sinnbeck's avatar

@freemium You can add a mutator to always add a day to it when inserting

public function setYearMonthAttribute($value)
{
    return Carbon::parse($value)->startOfMonth()->toDateString();
}
Tray2's avatar

I suggest storing it as a proper date and then format it to your liking or as @sinnbeck suggest use a varchar field.

Please or to participate in this conversation.