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

eliyas5044's avatar

Carbon formatting date when creating post, but "Unexpected data found" when trying to update post

In my model =>

public function setBirthDateAttribute($date)
    {
        $this->attributes['birth_date'] = Carbon::createFromFormat('M d,Y',$date)->toDateString();
    }

    public function getBirthDateAttribute($value)
    {
        return Carbon::parse($value)->toFormattedDateString();
    }

after dd()

#attributes: array:11 [▼
    "name" => "Eliyas Hossain"
    "father_name" => null
    "mother_name" => null
    "gender" => "Male"
    "birth_date" => "2017-06-01"
]

In my controller =>

public function update(EmployeeFormUpdate $request,  EmployeePersonalDetail $personalDetail)
    {
        
        dd($personalDetail->birth_date = $request->input('birth_date'));

        return redirect('dashboard/employee')->with(['message'=>'Employee Updated Successfully.','type'=>'success']);
    }

after hit submit button InvalidArgumentException in Carbon.php line 582: Unexpected data found. in Carbon.php line 582 at Carbon::createFromFormat('M d,Y', 'Jun 1, 2017') in EmployeePersonalDetail.php line 65 at EmployeePersonalDetail->setBirthDateAttribute('Jun 2, 2017') in HasAttributes.php line 519 at Model->setAttribute('birth_date', 'Jun 2, 2017') in Model.php line 1249 at Model->__set('birth_date', 'Jun 2, 2017') in EmployeeController.php line 155

Please someone help me to solve this problem.

0 likes
3 replies
tisuchi's avatar

I don't know why exactly you are manipulating dates inside method. Just simply define your column as a carbon instance in your model-

protected $dates = ['birth_date', 'some_more'];
eliyas5044's avatar
eliyas5044
OP
Best Answer
Level 3

I solved my problem :) Here is the code in my model =>

public function setBirthDateAttribute($date)
{
    $this->attributes['birth_date'] = date("Y-m-d",strtotime($date));
}

public function getBirthDateAttribute($value)
{
    return Carbon::parse($value)->toFormattedDateString();
}
eliyas5044's avatar

@tisuchi Thanks for your reply. You are right, but in my case, i am using bootstrap-datepicker to input date in form. If i use

protected $dates = ['birth_date', 'some_more'];

It works when i create post. but when try to update post via model binding in form, it show date and time both like this Jun 3, 2017 00.00.00. So after submit form, Carbon throw another error, because i creating date from only date not time. btw, i solved it by using php date() function.

Please or to participate in this conversation.