首页 > 解决方案 > PHP 注意:数组到字符串的转换;数据库工厂

问题描述

我有一个人才模型,可以接受许多我想使用数据工厂填充的教育。但是使用工匠修补程序填充教育数据会导致“数组到字符串转换”。据我所见,我没有提供要转换为字符串的数组。以下是教育的模式、迁移和工厂

错误信息

PHP Notice: Array to string conversion in C:/Core/.../vendor/laravel/framework/src/Illuminate/Support/Str.php on line 360

运行此语句时收到

$talents->each( function($talent) { factory(App\Education::class)->create(['talent_id' => $talent->id]); })

class Education extends Model
{
    protected $fillable = [
        'talent_id',
        'institution',
        'education_level',
        'other_education_level',
        'qualification_field',
        'start_date_month',
        'start_date_year',
        'end_date_month',
        'end_date_year',
    ];

    public function talent() {
        return $this->belongsTo(Talent::class);
    }
}
Schema::create('educations', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->unsignedBigInteger('talent_id')->index();
    $table->string('institution');
    $table->string('education_level');
    $table->string('other_education_level')->nullable();
    $table->string('qualification_field');
    $table->unsignedTinyInteger('start_date_month');
    $table->unsignedSmallInteger('start_date_year');
    $table->unsignedTinyInteger('end_date_month')->nullable();
    $table->unsignedSmallInteger('end_date_year')->nullable();
    $table->timestamps();
});
$factory->define(Education::class, function (Faker $faker) {
    return [
        'talent_id' => factory(\App\Talent::class),
        'institution' => $faker->word,
        'education_level' => $faker->word,
        'other_education_level' => $faker->word,
        'qualification_field' => $faker->words,
        'start_date_month' => rand(1, 12),
        'start_date_year' => rand(1970, 2000),
        'end_date_month' => rand(1, 12),
        'end_date_year' => rand(1970, 2000),
    ];
});

以下是我运行的修补程序命令

$talents = App\Talent::all()
$talents->each( function($talent) { factory(App\Education::class)->create(['talent_id' => $talent->id]); })

$talents->each( function($talent) { factory(App\Education::class)->create(['talent_id' => $talent->id]); })是原因,但我不明白为什么

例如,具有不同类模型的相同命令可以工作

$talents->each( function($talent) { factory(App\WorkExperience::class)->create(['talent_id' => $talent->id]); })
class WorkExperience extends Model
{
    protected $fillable = [
        'talent_id',
        'title',
        'employment_type',
        'company',
        'start_date_month',
        'start_date_year',
        'end_date_month',
        'end_date_year',
        'description',
    ];

    public function talent() {
        return $this->belongsTo(Talent::class);
    }
}
Schema::create('work_experiences', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->unsignedBigInteger('talent_id')->index();
    $table->string('title');
    $table->string('employment_type');
    $table->string('company');
    $table->unsignedTinyInteger('start_date_month');
    $table->unsignedSmallInteger('start_date_year');
    $table->unsignedTinyInteger('end_date_month')->nullable();
    $table->unsignedSmallInteger('end_date_year')->nullable();
    $table->text('description');
    $table->timestamps();
});
$factory->define(WorkExperience::class, function (Faker $faker) {
    return [
        'talent_id' => factory(\App\Talent::class),
        'title' => $faker->word,
        'employment_type' => $faker->word(['Internship', 'Part Time', 'Full Time']),
        'company' => $faker->word,
        'start_date_month' => rand(1, 12),
        'start_date_year' => rand(1970, 2000),
        'end_date_month' => rand(1, 12),
        'end_date_year' => rand(1970, 2000),
        'description' => $faker->paragraph,
    ];
});

标签: phplaravellaravel-6fakerfactories

解决方案


您的问题可能在于这段代码:

'talent_id' => factory(\App\Talent::class),

工厂方法将为 Talent 类返回一个数据数组,并尝试将其应用于talent_id, 并失败。

要纠正您的问题,请执行以下操作:

'talent_id' => function () { return factory(\App\Talent::class)->create()->id; }

推荐阅读