PHP 注意:数组转字符串;数据库工厂
PHP Notice: Array to string conversion; Database Factories
我有一个人才模型,它可以包含我想使用数据工厂填充的许多教育。但是使用 artisan tinker 填充 education 数据导致 "Array to string conversion"。据我所知,我没有给出要转换为字符串的数组。以下是教育的模型、迁移和工厂
错误信息
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]); })
是原因,但我不明白为什么
同一个命令用不同的 class 模型工作,例如
$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,
];
});
你的问题可能出在这段代码上:
'talent_id' => factory(\App\Talent::class),
工厂方法将 return 人才 class 的数据数组,并将尝试将其应用于 talent_id
,但失败。
要解决您的问题,请执行以下操作:
'talent_id' => function () { return factory(\App\Talent::class)->create()->id; }
这个问题最初是从 Laracasts website 论坛发布的,我刚刚收到了解决方案。
问题出在我的 EducationFactory:
'qualification_field' => $faker->words
$faker->words return 一个字符串数组。解决方法是使用$faker->sentence
'qualification_field' => $faker->sentence
我有一个人才模型,它可以包含我想使用数据工厂填充的许多教育。但是使用 artisan tinker 填充 education 数据导致 "Array to string conversion"。据我所知,我没有给出要转换为字符串的数组。以下是教育的模型、迁移和工厂
错误信息
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]); })
是原因,但我不明白为什么
同一个命令用不同的 class 模型工作,例如
$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,
];
});
你的问题可能出在这段代码上:
'talent_id' => factory(\App\Talent::class),
工厂方法将 return 人才 class 的数据数组,并将尝试将其应用于 talent_id
,但失败。
要解决您的问题,请执行以下操作:
'talent_id' => function () { return factory(\App\Talent::class)->create()->id; }
这个问题最初是从 Laracasts website 论坛发布的,我刚刚收到了解决方案。
问题出在我的 EducationFactory:
'qualification_field' => $faker->words
$faker->words return 一个字符串数组。解决方法是使用$faker->sentence
'qualification_field' => $faker->sentence