title: Laravel : using model factories to generate one-to-many relationships
body: <p>I have a problem with generating multiple one-to-many relationship for a model using factories in Laravel. The factories seem to only be generating one ClubFixture per Club, when they should be generating 5 ClubFixtures per Club.</p>
  
  <p><strong>Models</strong></p>
  
  <p><strong>Club</strong></p>
  
  <pre><code>&lt;?php
  
  namespace App;
  
  use Illuminate\Database\Eloquent\Model;
  
  class Club extends Model
  {
    //Table associated with the model
    protected $table = 'clubs';
  
     protected $fillable = ['name', 'league', 'age_group', 'colour', 'county', 'country'];
  
     public function fixtures(){
       return $this-&gt;hasMany('App\ClubFixture', 'club_id', 'id');
     }
  }
  </code></pre>
  
  <p><strong>ClubFixture</strong>
      
  
  <pre><code>namespace App;
  
  use Illuminate\Database\Eloquent\Model;
  
  class ClubFixture extends Model
  {
    //Table associated with the model
    protected $table = 'club_fixtures';
  }
  </code></pre>
  
  <p><strong>Modal Factories</strong></p>
  
  <pre><code>$factory-&gt;define(App\Club::class, function (Faker\Generator $faker) {
      return [
          'name' =&gt; $faker-&gt;name,
          'league' =&gt; $faker-&gt;word,
          'age_group' =&gt; $faker-&gt;word,
          'colour' =&gt; $faker-&gt;hexcolor,
          'county' =&gt; $faker-&gt;state,
          'country' =&gt; $faker-&gt;country,
          'emblem' =&gt; $faker-&gt;imageUrl(80, 80),
          'banner' =&gt; $faker-&gt;imageUrl,
          'matches' =&gt; $faker-&gt;randomDigit,
          'wins' =&gt; $faker-&gt;randomDigit,
          'losses' =&gt; $faker-&gt;randomDigit,
      ];
  });
  
  $factory-&gt;define(App\ClubFixture::class, function (Faker\Generator $faker) {
      return [
          'club_id' =&gt; function () {
              return factory(App\Club::class)-&gt;create()-&gt;id;
          },
          'opposition' =&gt; $faker-&gt;name,
          'address' =&gt; $faker-&gt;address,
          'datetime' =&gt; $faker-&gt;dateTimeBetween('now', '+30 weeks'),
          'type' =&gt; $faker-&gt;randomElement(['home', 'away', 'none']),
      ];
  });
  </code></pre>
  
  <p><strong>Database seeder</strong></p>
  
  <pre><code>factory(App\Club::class, 100)-&gt;create()-&gt;each(function ($u) {
       factory(App\ClubFixture::class, 5)-&gt;create();
  });
  </code></pre>
  
  <p><strong>Expected Result:</strong> Each Club should have 5 ClubFixtures associated with it</p>
  
  <p><strong>Actual Result:</strong> Some Clubs have no ClubFixtures, some only have one.</p>
  
  <p>I've tried this <a href="https://stackoverflow.com/questions/32540845/adding-relations-to-laravel-factory-model">answer</a>, but get an error with saveMany does not exist and relationship is null.</p>
  
  <p>You can download the SQL resulting database <a href="https://drive.google.com/open?id=1WD6xJiycd84PtvFBNFSNIjFMW7XAUYCW" rel="noreferrer">here</a>.</p>
  
  <p>Can someone tell me what I am doing wrong here?</p>