Search code examples
phplaravellaravel-8php-carbon

Laravel 8: Error Call to undefined method DateTime::set() while saving record


I recently update my laravel 8 and It seems like Carbon or date request is not working properly I am getting the following error while saving the record with date time

exception: "Error"
file:     "/../vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php"
line: 848
message: "Call to undefined method DateTime::set()"

Controller

$startDate = $request->start_date_time;
$events->start_date_time = Carbon::parse($startDate);

Parsing string: 2021-07-14T07:43:27.498Z

Version

PHP 7.4.21, Laravel 8


Solution

  • The issue is with casts

    'start_date_time' => 'dateTime',
    'end_date_time' => 'dateTime'
    

    so it should be

    'start_date_time' => 'datetime',
    'end_date_time' => 'datetime'
    

    Ref:https://laravel.com/docs/8.x/eloquent-mutators#attribute-casting