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

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

我是 Laravel 的新手,我使用的是 5.8.38 版。我在我的项目中使用它的身份验证系统,但是每当我使用“重置密码”选项时,在我输入将要发送重置密码电子邮件的电子邮件地址后。我收到以下错误:

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, y$kCwg25dPXcmsn4msea37FOD3ocpHHv1.q1A89dNfbMDADsOnNOole, 1616819329600))

我一直在尝试解决此问题,但我无法找到与此错误相关的任何内容,关于 Laravel 的 password_resets

我不知道为什么“created_at”的日期值被保存为“1616819329600”,不知道有什么办法可以解决这个问题。根据我的研究,我认为问题存在于 vendor\laravel\framework\src\Illuminate\Auth\Passwords\DatabaseTokenRepository.php 的文件 DatabaseTokenRepository 中,但我知道我不应该操纵这些文件。

如果有用,我把那个文件的内容分享给你:

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

非常感谢任何帮助!

你必须这样使用

use Carbon/Carbon;

在 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')];
}

我终于弄明白了,created_at 字段试图用 1616819329600 的值保存,因为 Laravel 的原始密码服务提供商之间存在冲突,和 Jenssegers 的密码服务提供商(在我将我的项目从 MongoDB 迁移到 MySQL 之前正在使用)位于“config/app.php”.

因此,当我的问题出现时,我的 app.php 文件如下所示:

(...)

/*
* Package Service Providers...
*/

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

(...)

最后,在修复之后,我删除了那些代码行,我的 app.php 现在看起来是这样的:

(...)

/*
* Package Service Providers...
*/


/*
* Application Service Providers...
*/

(...)

我猜你会说发生这种情况的确切原因是你不应该在同一个文件中使用两个以相同名称结尾的包服务提供商,正如你在以下示例中看到的那样:

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