Christian Giupponi
Christian Giupponi

Reputation: 7618

Laravel carbon always set to 0000-00-00

I need to store an expiration date in my database to check if the user account is still valid when he/she try to log in.

This is my User model:

class User extends Model implements AuthenticatableContract, CanResetPasswordContract {

    use Authenticatable, CanResetPassword;
    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';

    protected $dates = [
        'expiration_date'
    ];

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'first_name',
        'last_name',
        'email',
        'password',
        'expiration_date'
    ];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = [
        'password',
        'remember_token'
    ];

    /**
     * Questa funzione fa in modo di criptare in automatico la password quando si crea un utente
     *
     * @param $password
     *
     * @return string
     */
    public function setPAsswordAttribute( $password )
    {
        return $this->attributes['password'] = bcrypt( $password );
    }

    public function setExpirationDateAttribute( $expiration )
    {
        return $this->attributes['expiration_date'] = Carbon::createFromDate('Y-m-d', $expiration);
    }
}

In my form I have a text input that uses datepicker to set a tabe in dd/mm/yyyy format.
When I press submit in the DB I get only 0000-00-00 00:00:00

What is the problem?

Upvotes: 0

Views: 2212

Answers (1)

Giedrius
Giedrius

Reputation: 633

As you can see from the code, Carbon::createFromDate($year = null, $month = null, $day = null, $tz = null) method accepts 4 parameters and you are passing invalid arguments to it.

I believe that the method you are looking for is Carbon::createFromFormat($format, $time, $tz) which accepts format, string with date or time and DateTimeZone instance or a string timezone as parameters.

Upvotes: 1

Related Questions