Laravel-activitylog: Problem with date format in Model

Created on 21 Jan 2020  路  2Comments  路  Source: spatie/laravel-activitylog

I got error when trying to log activity with date format in my model.
protected static $logAttributes = ['expiration_date']

public function getExpirationDateAttribute($value)
    {
        if($value): 
            return \DateTime::createFromFormat($this->getDateFormat(), $value)->format('Y-m-d'); 
        endif;

    }

Symfony Component Debug Exception FatalThrowableError (E_ERROR)
Call to a member function format() on bool
How solve this problem ?

question

All 2 comments

It seems like the datetime create method returns false instead of a datetime instance.
I recommend you to use the available Laravel logic like $casts, Carbon and the already available methods to handle dates on the model like asDateTime() and so on.

https://github.com/spatie/laravel-activitylog/blob/195887d440ba4664fdea4c2fb8fbc67adb34a291/src/Traits/DetectsChanges.php#L138-L145

This is how we handle dates in this package - but I really recommend you to let Laravel cast this attribute and use the format method in your views or API resources who are responsible for displaying/formatting values in the wanted format.

I got this. Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mtuchi picture mtuchi  路  3Comments

ianrussel picture ianrussel  路  5Comments

abdosaeedelhassan picture abdosaeedelhassan  路  4Comments

damosse31 picture damosse31  路  5Comments

rjcrystal picture rjcrystal  路  5Comments