skip to Main Content

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

2

Answers


  1. Chosen as BEST ANSWER

    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',
    ]
    

  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search