title: Seeding relational data in Laravel eloquent database seeder / factory - Type error - Arg 1 should be Eloquent\Model - Collection given
body: <p>I am trying to seed a table with relational data in a Laravel 5.5 application.</p>
  
  <p>I have these two tables/models:</p>
  
  <ul>
  <li>User</li>
  <li>Question</li>
  </ul>
  
  <p>On the <strong>app\User.php</strong> model file; I have the following <code>hasMany</code> relationship:</p>
  
  <pre><code>public function questions()
  {
      return $this-&gt;hasMany(Question::class);
  }
  </code></pre>
  
  <p>And Here's my <strong>database/factories/QuestionFactory.php</strong></p>
  
  <pre><code>&lt;?php
  
  use Faker\Generator as Faker;
  
  $factory-&gt;define(App\Question::class, function (Faker $faker) {
      static $user_id;
  
      return [
          'user_id' =&gt; $user_id,
          'subject' =&gt; $faker-&gt;sentence(15),
          'body' =&gt; $faker-&gt;paragraph(3)
      ];
  });
  </code></pre>
  
  <p>and my <strong>database/factories/UserFactory.php</strong></p>
  
  <pre><code>&lt;?php
  
  use Faker\Generator as Faker;
  
  $factory-&gt;define(App\User::class, function (Faker $faker) {
      static $password;
  
      return [
          'name' =&gt; $faker-&gt;name,
          'email' =&gt; $faker-&gt;unique()-&gt;safeEmail,
          'password' =&gt; $password ?: $password = bcrypt('123456'),
          'remember_token' =&gt; str_random(10),
      ];
  });
  </code></pre>
  
  <p>Given the above; I am trying to use it all in my dummy data seeder like this:</p>
  
  <pre><code>class DummyDataSeeder extends Seeder
  {
      public function run()
      {
          // Seed dummy users
          factory(App\User::class, 10)-&gt;create()-&gt;each(function($user)
          {
              // With dummy questions
              $user-&gt;questions()-&gt;save(factory(App\Question::class, 3)-&gt;make());
          });
      }
  }
  </code></pre>
  
  <blockquote>
    <p>The goal was to create 10 dummy users, and for each users to have 3 questions each.</p>
  </blockquote>
  
  <p>When I seed the database with the above setup; I am getting the following error:</p>
  
  <blockquote>
    <p>[Symfony\Component\Debug\Exception\FatalThrowableError] Type error:
    Argument 1 passed to
    Illuminate\Database\Eloquent\Relations\HasOneOrMany::save() must be an
    instance of Illuminate\Database\Eloquent\Model, instance of
    Illuminate\Database\Eloquent\Collection given, called in
    C:\xampp\htdocs\myapp.local\database\seeds\DummyDataSeeder.php on line
    18</p>
  </blockquote>