PHP注意:数组到字符串的转换;数据库工厂

问题描述 投票:0回答:2

我有一个人才模型,可以使用数据工厂进行很多教育。但是,使用artisan tinkereducation数据进行填充会导致“数组到字符串转换”。从我看来,我没有给出要转换为字符串的数组。下面是教育的模型,迁移和工厂

错误消息

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,
    ];
});
php laravel laravel-6 faker factories
2个回答
0
投票

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

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

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

要解决您的问题,请执行:

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

0
投票

此问题最初是从Laracasts website论坛发布的,我刚刚收到了解决方案。

问题出在我的EducationFactory:

'qualification_field' => $faker->words

$ faker-> words返回一个字符串数组。解决方法是使用$ faker-> sentence

'qualification_field' => $faker->sentence
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.