【问题标题】:ErrorException : Array to string conversion during php artisan migrate:freshErrorException:php artisan migrate:fresh 期间的数组到字符串转换
【发布时间】:2019-09-20 00:46:35
【问题描述】:

尝试将列设置为 dateTime 类型,并默认为当前时间和日期。但是,我不断收到标题中提到的错误...

这个问题与“dateAccepted”字段有关,“dateSubmitted”可能也会有这个问题,但我还没有尝试过。

我是否使用了错误的数据类型?或者只是编码错误..?欢迎所有建议。 (P.S 新网站,为格式化道歉)

public function up()
{
    Schema::create('adoption_requests', function (Blueprint $table) {
        $table->increments('id');
        $table->bigInteger('userid')->unsigned();
        $table->bigInteger('animalID')->unsigned();
        $table->date('dateSubmitted');
        $table->dateTime('dateAccepted')->default(getdate())->nullable();
        $table->date('requestAccepted');
        $table->date('staffID');
        $table->timestamps();
        //$table->boolean('$adoptionStatus')->default(0);
        $table->foreign('userid')->references('id')->on('users');
        $table->foreign('animalID')->references('id')->on('animals');

    });
}
    ErrorException  : Array to string conversion

  at C:\xampp\htdocs\AAS\vendor\laravel\framework\src\Illuminate\Database\Schema\Grammars\Grammar.php:248
    244|         }
    245|
    246|         return is_bool($value)
    247|                     ? "'".(int) $value."'"
  > 248|                     : "'".(string) $value."'";
    249|     }
    250|
    251|     /**
    252|      * Create an empty Doctrine DBAL TableDiff from the Blueprint.

  Exception trace:

  1   Illuminate\Foundation\Bootstrap\HandleExceptions::handleError("Array to string conversion", "C:\xampp\htdocs\AAS\vendor\laravel\framework\src\Illuminate\Database\Schema\Grammars\Grammar.php", [])
      C:\xampp\htdocs\AAS\vendor\laravel\framework\src\Illuminate\Database\Schema\Grammars\Grammar.php:248

  2   Illuminate\Database\Schema\Grammars\Grammar::getDefaultValue()
      C:\xampp\htdocs\AAS\vendor\laravel\framework\src\Illuminate\Database\Schema\Grammars\MySqlGrammar.php:946

  Please use the argument -v to see more details.

【问题讨论】:

    标签: laravel laravel-migrations


    【解决方案1】:
    $table->dateTime('dateAccepted')->default(DB::raw('CURRENT_TIMESTAMP'));
    

    这可能会有所帮助

    编辑

    解释:

    function gettime() 返回一个数组。因此,您不能在 datetime 列中存储值数组。你需要datetime

    与原始答案不同的另一种方法是,

    $table->dateTime('dateAccepted')->default(now()->toDateTimeString());
    

    【讨论】:

    • 请解释原因
    • getdate() 输出数组。并被分配给一个字符串的日期时间
    猜你喜欢
    • 2018-08-04
    • 2016-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-27
    • 2015-02-21
    相关资源
    最近更新 更多