title: Laravel 8 Multiple Relationships for Factory
body: <p>In Laravel 8 it is possible to quickly fill relationships with factories. However, I cannot figure out how to generate more than one relationship. <strong>How can I create a random or new relationship for each link using the new Laravel 8 syntax?</strong></p>
  <p>This factory syntax is only available in Laravel 8.
  <a href="https://laravel.com/docs/8.x/database-testing#factory-relationships" rel="nofollow noreferrer">https://laravel.com/docs/8.x/database-testing#factory-relationships</a></p>
  <h2>Problem</h2>
  <p>Consider the following relationship:</p>
  <ul>
  <li>Each link belongs to a website and a post.</li>
  <li>Both websites and posts can have many links.</li>
  </ul>
  <pre><code>&lt;?php
  
  class Post extends Model
  {
      use HasFactory;
  
      function links()
      {
          return $this-&gt;hasMany(Link::class);
      }
  }
  
  class Website extends Model
  {
      use HasFactory;
  
      function links()
      {
          return $this-&gt;hasMany(Link::class);
      }
  }
  
  class Link extends Model
  {
      use HasFactory;
  
      function post()
      {
          return $this-&gt;belongsTo(Post::class);
      }
  
      function website()
      {
          return $this-&gt;belongsTo(Website::class);
      }
  }
  
  </code></pre>
  <hr />
  <h2>What I tried/want</h2>
  <p>What I tried below will only generate one model for all the links. <strong>How can I create a random or new relationship for each link using the new Laravel 8 syntax?</strong></p>
  <pre><code>Link::factory()-&gt;count(3)-&gt;forPost()-&gt;forWebsite()-&gt;make()
  
  =&gt; Illuminate\Database\Eloquent\Collection {#4354
       all: [
         App\Models\Link {#4366
           post_id: 1,
           website_id: 1,
         },
         App\Models\Link {#4395
           post_id: 1, // return a different ID
           website_id: 1,
         },
         App\Models\Link {#4370
           post_id: 1, // return a different ID
           website_id: 1, // return a different ID
         },
       ],
     }
  </code></pre>