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

Neeraj1005's avatar

DateTime::__construct(): Failed to parse time string (Null) at position 0 (N): The timezone could not be found in the database

I'm trying to update the date field if it is Null or if it is not null then put the null in table. but problem is date is stored but null value is not stored. can anyone solve this problem?

#This is controller method
  public function taskcompleted(Request $request, $id)
        {
            $task = Todo::findOrFail($id);

            // if ($task->completed_at == Null) {

            //     $task->completed_at = Carbon::now();

            // }
            // else {

            //     $task->completed_at = 'Null';
            // }

            ($task->completed_at == Null) ? $task->completed_at = Carbon::now() : $task->completed_at = 'Null';

            $task->save();

            return back();
        }

#this is Model

<?php

namespace App;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Todo extends Model
{
    use SoftDeletes;

    //Table Name
    protected $table = 'todos';
    //Primary key
    public $primaryKey = 'id';

    protected $fillable = [

        'todoable_id',
        'todoable_type',
        'title',
        'description',
        'user_id',
        'priority',
        'outcome_id',
        'tasktype_id',
        'started_at',
        'due_time',
        'completed_at',
    ];

 	protected $casts = [
        'started_at' => 'datetime',
        'due_time' => 'datetime',
        'completed_at',
    ];

    protected $dates = [
        'started_at',
        'due_time',
        'completed_at',
        'created_at',
        'updated_at',
        'deleted_at',
    ];

    public function setDueTimeAttribute($date)
    {

        // $attributes['due_time'] = Carbon::createFromFormat('d-m-Y H:i:s', $date);
        $this->attributes['due_time'] = Carbon::parse($date)->format('Y-m-d H:i:s');
    }

    public function setStartedAtAttribute($date)
    {

        $this->attributes['started_at'] = Carbon::parse($date)->format('Y-m-d H:i:s');
    }

    public function setCompletedAtAttribute($date)
    {

        $this->attributes['completed_at'] = Carbon::parse($date)->format('Y-m-d H:i:s');
    }
0 likes
6 replies
MichalOravec's avatar
public function taskcompleted(Request $request, $id)
{
    Todo::findOrFail($id)->update([
        'completed_at' => now()
    ]);

    return back();
}

Also I don't think that you need setCompletedAtAttribute in your model.

Main problem was that you tried parse string 'Null' in Carbon

Neeraj1005's avatar

@michaloravec nothing is changed.. only timer increases. I want to put the Null if there is completed_at date. Like complete or not

Neeraj1005's avatar

@michaloravec so what I have to do? should I remove this line?

 public function setCompletedAtAttribute($date)
    {

        $this->attributes['completed_at'] = Carbon::parse($date)->format('Y-m-d H:i:s');
    }

Edit: After removing this. THis is not also working.

MichalOravec's avatar
Level 75
public function taskcompleted(Request $request, $id)
{
    $task = Todo::findOrFail($id);

    $taks->update([
        'completed_at' => $task->completed_at ? null : now()
    ]);

    return back();
}

And removesetCompletedAtAttributemethod from your Todo model

Neeraj1005's avatar

@michaloravec working.. Thank you.. can you please tell why my method was not working? what was the problem with this?

 public function setCompletedAtAttribute($date)
    {

        $this->attributes['completed_at'] = Carbon::parse($date)->format('Y-m-d H:i:s');
    }
MichalOravec's avatar

Because you set it to string 'Null' and not to null, and if you use Carbon::parse() with null or 'Null' in your case, it throws an error.

1 like

Please or to participate in this conversation.