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

kickthemooon's avatar

allow carbon date field to be null

my class where I use carbon:

protected $dates = ['expireson'];

public function getExpiresonAttribute($value)
    {
        return Carbon::parse($value)->format('d.m.Y');
    }

    public function setExpiresonAttribute($value)
    {
        $this->attributes['expireson'] = Carbon::createFromFormat('d.m.Y', $value)->toDateString();
    }

my view:

<input type="text" id="expireson" name="expireson" class="form-control" value="{{ $offer->expireson }}">

<script type="text/javascript">
        jQuery(document).ready(function($) {
            $('#expireson').datetimepicker({
                format: 'DD.MM.YYYY'
            });
        });
    </script>

Now want to allow the user to delete the value of this field and update it in the db to null but carbon is throwing an error:

InvalidArgumentException in Carbon.php line 414:
Data missing

Any suggestions?

0 likes
2 replies
bobbybouwmann's avatar
Level 88

Well you say createFromFormat, but you are not passing any data to the function, so you need to do an extra check here

$this->attributes['expireson'] = $value ? Carbon::createFromFormat('d.m.Y', $value)->toDateString() : null;
5 likes

Please or to participate in this conversation.