title: How to create a model factory with foreign key laravel
body: <p>I have a model factory as:</p>
  
  <p><strong>QuestionFactory.php:</strong></p>
  
  <pre><code>$factory-&gt;define(App\Question::class, function (Faker $faker) {
      return [
          'question' =&gt; $faker-&gt;text($maxNbChars = 150),
          'subject_id' =&gt; $faker-&gt;randomElement(App\Subject::pluck('id')-&gt;toArray()),
          'sbj_type_id' =&gt; $faker-&gt;randomElement(App\SbjType::pluck('id')-&gt;toArray())
      ];
  });
  </code></pre>
  
  <p>but the above code also fetches the other subject type here<code>sbj_type_id</code> is linked to <code>sbj_type</code> table and then there's also a field that which a <code>sbj_type</code> relates, <strong>.e.g.</strong>, I have 2 subjects and 4 sub type so I have 2 type linked to 1 subject and other 2 relates to second subject so I need to fetch according to subject_id also, I have done this </p>
  
  <pre><code>'sbj_type_id' =&gt; $faker-&gt;randomElement(App\SbjType::where('subject_id', App\Subject::inRandomOrder()-&gt;value('id'))-&gt;pluck('id')-&gt;toArray())
  </code></pre>
  
  <p><strong>All I need to fetch <code>sbj_type</code> according to <code>subject_id</code></strong></p>