【问题标题】:Laravel seeder gets stuck and returns ErrorException Array yo string conversionLaravel 播种机卡住并返回 ErrorException 数组到字符串转换
【发布时间】:2021-07-05 04:23:43
【问题描述】:
public function up()
{
    Schema::create('settings', function (Blueprint $table) {
        $table->id();
        $table->string('name', 40)->unique();
        $table->json('value');
        $table->timestamps();
    });

    //seeder to insert FTP settings
    DB::table("settings")->insert([
        'name' => 'FTP_SETTINGS',
        'value' => ['host' => '192.168.5.190', 'username'=> 'Alessandro', 'password' => 'Alessandro', 'port' => '21']
    ]);
}

之后我正在使用播种机进行此迁移(我也将其放入播种机部分,但有同样的问题)但我得到了 ErrorException 数组到字符串的转换。 可能是具有价值的东西,但我不明白我做错了什么......非常感谢您的帮助。

【问题讨论】:

  • 你的值不是一个字符串,而是另一个数组 ['host' => '......', 'username'....... ]。它应该是一个字符串
  • 而不是'value' => ['host' => '192.168.5.190', 'username'=> 'Alessandro', 'password' => 'Alessandro', 'port' => '21'],您可以考虑将所有内容保存在不同的列中或执行'value' => json_encode(['host' => '192.168.5.190', 'username'=> 'Alessandro', 'password' => 'Alessandro', 'port' => '21'])

标签: laravel eloquent laravel-migrations laravel-seeding


【解决方案1】:

您正在尝试将数组值插入到 json 字段中。

Try instead: 

    DB::table("settings")->insert([
        'name' => 'FTP_SETTINGS',
        'value' => json_encode(['host' => '192.168.5.190', 'username'=> 'Alessandro', 'password' => 'Alessandro', 'port' => '21'])
    ]);

【讨论】:

    猜你喜欢
    • 2020-09-10
    • 2018-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多