I defined a factory in order to seed tables with faker. Following the official docs, I implemented it as showed, like this:
<?php
use Faker\Generator as Faker;
$factory->define(App\Item::class, function (Faker $faker) {
return [
'codice' => $faker->word,
'data_acquisto' => $faker->date('Y-m-d'),
[...] // Other stuff
// Relations
'componente_id' => function() {
return factory(App\Componente::class)->create()->id;
},
[...] // Other relations
];
});
When I launch it with php artisan migrate:refresh --seed, framework starts to seed the destination tables, but it being stuck in the relation seeding phase (factory(App\Componente::class)->create()->id). Apparently resulting in a some sort of infinite/recursive process (generating thousands of records), until the task crashes due to an overflow.
By googling around, I noticed different approaches from many devs, but seems that others experienced this problem like me. Someone tried to indicate the max number in the factory argument:
// Ex. 5 max
return factory(App\Componente::class, 5)->create()->id;
And the Laravel doc doesn't mention it. But in this way, it throws another exception:
Property [id] does not exist on this collection instance.
Either if the migration results like this:
Schema::create('componente', function (Blueprint $table) {
$table->increments('id'); // <- ID exists!
$table->string('tipo');
[...] // Other stuff
}
About this, I read about the collection specification, i.e. the use of first(). But I don't get how to avoid this scenario. I mean, if I use first() in the statement, how can it generates the number of required rows if first() returns only the first collection occurrence?
At last, I just need to create 3 rows of this one, so how can I approach it?
Thanks in advance everyone for help.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire