Level 88
Well, $faker->words returns an array of strings. So if you change this to $faker->word or $faker->sentence it should all work fine ;)
2 likes
I have a talent model which can have many educations which I want to populate using data factories. But populating the education section cause "Array to string conversion". From what I see, I am not giving an array to be converted into a string. Below are the Education's model, migration & factory
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),
];
});
Below are the tinker commands I ran
$talents = App\Talent::all()
$talents->each( function($talent) { factory(App\Education::class)->create(['talent_id' => $talent->id]); })
The $talents->each( function($talent) { factory(App\Education::class)->create(['talent_id' => $talent->id]); }) is the cause, but I don't understand why
Well, $faker->words returns an array of strings. So if you change this to $faker->word or $faker->sentence it should all work fine ;)
Please or to participate in this conversation.