title: Laravel generate object using model factory with parameter
body: <p>I have this model factory that generates new contact, it uses random <code>company_id</code>: </p>
  
  <pre><code>$factory-&gt;define(App\Models\Contact::class, function (Faker\Generator $faker) 
  {
      $company_id = Company::all()-&gt;random()-&gt;id;
  
      return [
          'firstname' =&gt; $faker-&gt;firstName,
          'lastname' =&gt; $faker-&gt;lastName,
          'phone' =&gt; $faker-&gt;phoneNumber,
          'email' =&gt; $faker-&gt;email,
          'company_id' =&gt; $company_id,
          'lead_id' =&gt; \App\Models\Lead::where('company_id', $company_id)-&gt;get()-&gt;random()-&gt;id,
      ];
  });
  </code></pre>
  
  <p>It is ok when I use it in seeds:</p>
  
  <pre><code>factory(App\Models\Contact::class)-&gt;create();
  </code></pre>
  
  <p>But for testing I need somehow to pass <code>$company_id</code> to factory, create contact for concrete <code>company_id</code> (I know that I can do <code>-&gt;create(['company_id', $company_id])</code>) but this will rewrite only company_id from Contact.</p>
  
  <p>When I select <code>lead_id</code>, I also need to know current <code>company_id</code>.</p>
  
  <p>How to pass company_id to factory as parameter? </p>