Muhammad Owais
Muhammad Owais

Reputation: 1186

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

Upvotes: 1

Views: 2576

Answers (2)

Muhammad Owais
Muhammad Owais

Reputation: 1186

Its resolved by changing my event model

Before

protected $casts = [
    'start_date_time' => 'dateTime',
    'end_date_time' => 'dateTime',
]

After

protected $casts = [
    'start_date_time' => 'datetime',
    'end_date_time' => 'datetime',
]

Upvotes: 4

John Lobo
John Lobo

Reputation: 15319

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

Upvotes: 10

Related Questions