简体   繁体   English

Laravel - SQLSTATE [22007]:无效的日期时间格式:1292 不正确的日期时间值:'1616818311712'

[英]Laravel - SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value: '1616818311712'

I am new to Laravel and I am using version 5.8.38.我是 Laravel 的新手,我使用的是 5.8.38 版本。 I am using its Authentication system in my project, but whenever I use the "Reset Password" option, after I enter the email address where the Reset Password email is going to be sent to.我在我的项目中使用它的身份验证系统,但是每当我使用“重置密码”选项时,在我输入重置密码 email 将被发送到的 email 地址后。 I get the following error:我收到以下错误:

SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value: '1616819329600' for column `proyectolaravel`.`password_resets`.`created_at` at row 1 (SQL: insert into `password_resets` (`email`, `token`, `created_at`) values (andre_jack@hotmail.com, $2y$10$kCwg25dPXcmsn4msea37FOD3ocpHHv1.q1A89dNfbMDADsOnNOole, 1616819329600))

I have been trying to fix this problem but I have not been able to find anything related to this error regarding password_resets for Laravel.我一直在尝试解决这个问题,但我找不到任何与此错误有关的关于 Laravel 的密码重置的任何信息。

I do not know why the date value for "created_at" is being saved as "1616819329600", and I wonder if there is any way to solve this.我不知道为什么“created_at”的日期值被保存为“1616819329600”,不知道有没有办法解决这个问题。 For what I have researched, I think the problem relays in the file DatabaseTokenRepository at vendor\laravel\framework\src\Illuminate\Auth\Passwords\DatabaseTokenRepository.php but I know that I should not manipulate those files.对于我所研究的内容,我认为问题出现在vendor\laravel\framework\src\Illuminate\Auth\Passwords\DatabaseTokenRepository.php DatabaseTokenRepository DatabaseTokenRepository 文件中,但我知道我不应该操纵这些文件。

If it is of any use, I share with you the content of that file:如果有任何用处,我与您分享该文件的内容:

<?php

namespace Illuminate\Auth\Passwords;

use Illuminate\Support\Str;
use Illuminate\Support\Carbon;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Contracts\Hashing\Hasher as HasherContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class DatabaseTokenRepository implements TokenRepositoryInterface
{
    /**
     * The database connection instance.
     *
     * @var \Illuminate\Database\ConnectionInterface
     */
    protected $connection;

    /**
     * The Hasher implementation.
     *
     * @var \Illuminate\Contracts\Hashing\Hasher
     */
    protected $hasher;

    /**
     * The token database table.
     *
     * @var string
     */
    protected $table;

    /**
     * The hashing key.
     *
     * @var string
     */
    protected $hashKey;

    /**
     * The number of seconds a token should last.
     *
     * @var int
     */
    protected $expires;

    /**
     * Create a new token repository instance.
     *
     * @param  \Illuminate\Database\ConnectionInterface  $connection
     * @param  \Illuminate\Contracts\Hashing\Hasher  $hasher
     * @param  string  $table
     * @param  string  $hashKey
     * @param  int  $expires
     * @return void
     */
    public function __construct(ConnectionInterface $connection, HasherContract $hasher,
                                $table, $hashKey, $expires = 60)
    {
        $this->table = $table;
        $this->hasher = $hasher;
        $this->hashKey = $hashKey;
        $this->expires = $expires * 60;
        $this->connection = $connection;
    }

    /**
     * Create a new token record.
     *
     * @param  \Illuminate\Contracts\Auth\CanResetPassword  $user
     * @return string
     */
    public function create(CanResetPasswordContract $user)
    {
        $email = $user->getEmailForPasswordReset();

        $this->deleteExisting($user);

        // We will create a new, random token for the user so that we can e-mail them
        // a safe link to the password reset form. Then we will insert a record in
        // the database so that we can verify the token within the actual reset.
        $token = $this->createNewToken();

        $this->getTable()->insert($this->getPayload($email, $token));

        return $token;
    }

    /**
     * Delete all existing reset tokens from the database.
     *
     * @param  \Illuminate\Contracts\Auth\CanResetPassword  $user
     * @return int
     */
    protected function deleteExisting(CanResetPasswordContract $user)
    {
        return $this->getTable()->where('email', $user->getEmailForPasswordReset())->delete();
    }

    /**
     * Build the record payload for the table.
     *
     * @param  string  $email
     * @param  string  $token
     * @return array
     */
    protected function getPayload($email, $token)
    {
        return ['email' => $email, 'token' => $this->hasher->make($token), 'created_at' => new Carbon];
    }

    /**
     * Determine if a token record exists and is valid.
     *
     * @param  \Illuminate\Contracts\Auth\CanResetPassword  $user
     * @param  string  $token
     * @return bool
     */
    public function exists(CanResetPasswordContract $user, $token)
    {
        $record = (array) $this->getTable()->where(
            'email', $user->getEmailForPasswordReset()
        )->first();

        return $record &&
               ! $this->tokenExpired($record['created_at']) &&
                 $this->hasher->check($token, $record['token']);
    }

    /**
     * Determine if the token has expired.
     *
     * @param  string  $createdAt
     * @return bool
     */
    protected function tokenExpired($createdAt)
    {
        return Carbon::parse($createdAt)->addSeconds($this->expires)->isPast();
    }

    /**
     * Delete a token record by user.
     *
     * @param  \Illuminate\Contracts\Auth\CanResetPassword  $user
     * @return void
     */
    public function delete(CanResetPasswordContract $user)
    {
        $this->deleteExisting($user);
    }

    /**
     * Delete expired tokens.
     *
     * @return void
     */
    public function deleteExpired()
    {
        $expiredAt = Carbon::now()->subSeconds($this->expires);

        $this->getTable()->where('created_at', '<', $expiredAt)->delete();
    }

    /**
     * Create a new token for the user.
     *
     * @return string
     */
    public function createNewToken()
    {
        return hash_hmac('sha256', Str::random(40), $this->hashKey);
    }

    /**
     * Get the database connection instance.
     *
     * @return \Illuminate\Database\ConnectionInterface
     */
    public function getConnection()
    {
        return $this->connection;
    }

    /**
     * Begin a new database query against the table.
     *
     * @return \Illuminate\Database\Query\Builder
     */
    protected function getTable()
    {
        return $this->connection->table($this->table);
    }

    /**
     * Get the hasher instance.
     *
     * @return \Illuminate\Contracts\Hashing\Hasher
     */
    public function getHasher()
    {
        return $this->hasher;
    }
}

Any help will be highly appreciated!任何帮助将不胜感激!

You have to use like this你必须像这样使用

use Carbon/Carbon;

Add above in header section在 header 部分添加以上内容

protected function getPayload($email, $token)
{
    return ['email' => $email, 'token' => $this->hasher->make($token), 'created_at' => Carbon::now()->format('Y-m-d H:i:s')];
}

I FINALLY figured it out, the created_at field was trying to be saved with the value of 1616819329600 because there was a conflict between Laravel's original Password Service Provider, and Jenssegers' Password Service Provider (which was being used before I migrated my project from MongoDB to MySQL) at "config/app.php".我终于想通了, created_at字段试图用1616819329600的值保存,因为 Laravel 的原始密码服务提供商和 Jenssegers 的密码服务提供商(在我将项目从 MongoDB 迁移到MySQL)在“config/app.php”。

So, when my problem was ocurring, my app.php file looked like this:所以,当我的问题发生时,我的app.php文件看起来像这样:

(...)

/*
* Package Service Providers...
*/

Jenssegers\Mongodb\MongodbServiceProvider::class,
Jenssegers\Mongodb\Auth\PasswordResetServiceProvider::class,

(...)

And finally, after the fix, I just deleted those lines of code and my app.php now looks this way:最后,在修复之后,我刚刚删除了这些代码行,我的app.php现在看起来像这样:

(...)

/*
* Package Service Providers...
*/


/*
* Application Service Providers...
*/

(...)

I guess you could say that the exact reason this happened is that you shouldn't use two package service providers ending with the same name at the same file, as you can see in the following example:我猜你可能会说发生这种情况的确切原因是你不应该在同一个文件中使用两个以相同名称结尾的 package 服务提供者,如下例所示:

Illuminate\Auth\Passwords\PasswordResetServiceProvider::class,
Jenssegers\Mongodb\Auth\PasswordResetServiceProvider::class,

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQLSTATE[22007]:无效的日期时间格式:1292 日期时间值不正确 - SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value Laravel 将时间戳保存为 DateTime - SQLSTATE[22007]:日期时间格式无效:1292 日期时间值不正确: - Timestamp being saved as DateTime by Laravel - SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value: LARAVEL:SQLSTATE [22007]:无效的日期时间格式:1292 截断不正确的 DOUBLE 值 - LARAVEL : SQLSTATE[22007]: Invalid datetime format: 1292 Truncated incorrect DOUBLE value Laravel 8:SQLSTATE[22007]:无效的日期时间格式:1366 不正确的 integer 值 - Laravel 8: SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value 如何修复&#39;SQLSTATE [22007]:无效的日期时间格式:1292截断了错误的DOUBLE值:&#39;X&#39; - How to fix 'SQLSTATE[22007]: Invalid datetime format: 1292 Truncated incorrect DOUBLE value: 'X'' Eloquent 删除 - SQLSTATE [22007]:无效的日期时间格式:1292 截断不正确的 DOUBLE 值: - Eloquent Delete - SQLSTATE[22007]: Invalid datetime format: 1292 Truncated incorrect DOUBLE value: SQLSTATE [22007]:日期时间格式无效:1292 日期时间值不正确:“fotostudio”列的“28-01-2022 12:00”。“transaction_details” - SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value: '28-01-2022 12:00' for column `fotostudio`.`transaction_details` SQLSTATE [22007]:无效的日期时间格式:1292错误的日期时间值:第1行的“ created_at”列为“ 1970-01-01 00:00:01” - SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value: '1970-01-01 00:00:01' for column 'created_at' at row 1 “SQLSTATE[22007]:无效的日期时间格式:1292 不正确的日期时间值:&#39;2018&#39;列&#39;created_at&#39;在第 1 行(SQL:插入到`news` - "SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value: '2018' for column 'created_at' at row 1 (SQL: insert into `news` SQLSTATE [22007]:无效的日期时间格式:1366不正确的整数值 - SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM