Laravel - Seeding Relationships : <p>In Laravel, database seeding is generally accomplished through Model factories.  So you define a blueprint for your Model using Faker data, and say how many instances you need:</p>

<pre><code>$factory-&gt;define(App\User::class, function (Faker\Generator $faker) {
    return [
        'name' =&gt; $faker-&gt;name,
        'email' =&gt; $faker-&gt;email,
        'password' =&gt; bcrypt(str_random(10)),
        'remember_token' =&gt; str_random(10),
    ];
});

$user = factory(App\User::class, 50)-&gt;create();
</code></pre>

<p>However, lets say your User model has a <code>hasMany</code> relationship with many other Models, like a <code>Post</code> model for example:</p>

<pre><code>Post:
   id
   name
   body
   user_id
</code></pre>

<p>So in this situation, you want to seed your Posts table with <strong><em>actual</em></strong> users that were seeded in your Users table.  This doesn't seem to be explicitly discussed, but I did find the following in the Laravel docs:</p>

<pre><code>$users = factory(App\User::class, 3)
    -&gt;create()
    -&gt;each(function($u) {
         $u-&gt;posts()-&gt;save(factory(App\Post::class)-&gt;make());
    });
</code></pre>

<p>So in your User factory, you create X number of Posts for each User you create.  However, in a large application where maybe 50 - 75 Models share relationships with the User Model, your User Seeder would essentially end up seeding the entire database with all it's relationships.</p>

<p>My question is:  Is this the best way to handle this?  The only other thing I can think of is to Seed the Users first (without seeding any relations), and then pull random Users from the DB as needed while you are seeding other Models.  However, in cases where they need to be unique, you'd have to keep track of which Users had been used.  Also, it seems this would add a lot of extra query-bulk to the seeding process.</p>
