Laravel 9运行播种程序时发生阵列到字符串转换错误

2eafrhcq  于 2022-12-05  发布在  其他
关注(0)|答案(1)|浏览(86)

运行播种机时,我在Laravel 9上遇到错误,它显示为Array to string conversion
我在这个DataMaster表之前有一个相同的seeder类型json,它工作正常。但是当我运行DataMasterSeeder时,它不工作
我的播种机:

<?php

namespace Database\Seeders;

use App\Models\DataMaster;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;

class DataMasterSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        //SDU
        DataMaster::create(['formId' => 1, 'userId' => 1, 'kecamatanId' => 1, 'desaId' => null, 'fieldDatas' => [['id' => '1', 'name' => 'jumlah', 'title' => 'Jumlah', 'value' => '4605']], 'level' => 'kecamatan']);
    }
}

我的DataMaster迁移:

public function up()
    {
        Schema::create('data_masters', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('formId');
            $table->unsignedBigInteger('userId');
            $table->unsignedBigInteger('kecamatanId')->nullable();
            $table->unsignedBigInteger('desaId')->nullable();
            $table->json('fieldDatas');
            $table->enum('level', ['kecamatan', 'desa']);
            $table->timestamps();

            $table->foreign("formId")->references("id")->on("forms")->onDelete('cascade');
            $table->foreign("userId")->references("id")->on("users")->onDelete('cascade');
            $table->foreign("kecamatanId")->references("id")->on("kecamatans")->onDelete('cascade');
            $table->foreign("desaId")->references("id")->on("desas")->onDelete('cascade');
        });
    }

我有另一个种子,如fieldDatas json字段在这个DataMaster种子,我运行它成功之前,运行DataMaster种子。

huwehgph

huwehgph1#

您应该在插入fieldDatas字段之前对其进行编码

DataMaster::create([
    'formId' => 1,
    'userId' => 1,
    'kecamatanId' => 1,
    'desaId' => null,
    // here...
    'fieldDatas' => json_encode([['id' => '1', 'name' => 'jumlah', 'title' => 'Jumlah', 'value' => '4605']]),
    'level' => 'kecamatan',
]);

相关问题