0

我需要在我的数据库中存储一个过期日期,以检查他/她尝试登录时用户帐户是否仍然有效。

这是我的用户模型:

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);
    }
}

在我的表单中,我有一个使用 datepicker 设置dd/mm/yyyy格式的文本输入。
当我在数据库中按提交时,我只得到0000-00-00 00:00:00

问题是什么?

4

1 回答 1

1

正如您从代码中看到的,Carbon::createFromDate($year = null, $month = null, $day = null, $tz = null)方法接受 4 个参数,并且您正在向它传递无效参数。

我相信您正在寻找的方法是Carbon::createFromFormat($format, $time, $tz)接受格式、带有日期或时间的字符串以及 DateTimeZone 实例或字符串时区作为参数。

于 2015-02-08T10:50:04.477 回答