1

I have Articles with translations and I am trying to created a seeder for it.

I have a model factory to define a simple Article as follows:

$factory->define(\App\Article::class, function () {
    return [];
});

And ArticleTranslation

$factory->defineAs(\App\ArticleTranslation::class, 'en_US', function (Faker\Generator $faker) {
    $faker->addProvider(new Faker\Provider\en_US\Person($faker));

    return [
        'locale' => 'en',
        'title' => $faker->realText(40),
        'content' => $faker->realText(200),
    ];
});

I can use the article factory and for each one add a translation like this:

    factory(\App\Article::class, 10)->create()->each(function ($article) {
        $article->translations()->save(factory(\App\ArticleTranslation::class, 'en_US')->make());
    });

However I believe there must be a way to define the Article factory in a way that requests the translations itself.

How can I do this?

Hector Ordonez
  • 1,044
  • 1
  • 12
  • 20

0 Answers0