title: How to use Laravel's factory/faker with a model that has a constructor with parameters?
body: <p>I'm new to working with laravel's factory/faker dummy data generation. I'm curious as to how I can run my factory, from tinker, and have it use a model's constructor that (in this case) takes two integer parameters. </p>
  
  <p>My research, on S.O. and Googs, has failed to point me in the right direction for dealing with this particular situation. The code that follows is only for testing/edu, that's why I tried using a service provider (which could be the problem).</p>
  
  <p>Here's the set up, I have my model:</p>
  
  <p><strong>Doombot.php</strong></p>
  
  <pre><code>class Doombot extends Model
  {
      public $timestamps = false;
  
      public $model_id;
      public $weapon_id;
      public $name;
  
      public function __construct(int $model_id, int $weapon_id)
      {
          $this-&gt;model_id = $model_id;
          $this-&gt;weapon_id = $weapon_id;
      }
  }
  </code></pre>
  
  <p>I have a service provider to initialize a newly created (typehinted) Doombot, passing the constructors arguments: </p>
  
  <p><strong>DoombotsServiceProvider.php</strong></p>
  
  <pre><code>public function register()
  {
      $this-&gt;app-&gt;bind(Doombot::class, function(){
  
          $model_id = random_int(1, 2);
          $weapon_id = random_int(1, 2);
  
          return new Doombot($model_id, $weapon_id);
  
      });
  }
  </code></pre>
  
  <p>So now when I create a Doombot, I have the parameters set within my object. Great. Then I have my factory:</p>
  
  <p><strong>DoombotsFactory.php</strong></p>
  
  <pre><code>$factory-&gt;define(Doombot::class, function (Faker $faker) {
  
     return [
          'model_id' =&gt; $faker-&gt;numberBetween(1, 2),
          'weapon_id' =&gt; $faker-&gt;numberBetween(1, 2),
          'name' =&gt; $faker-&gt;firstName
      ];
  });
  </code></pre>
  
  <p>When I run the above in Tinker using:</p>
  
  <pre><code>factory(App\Doombot::class)-&gt;make()
  </code></pre>
  
  <p>I get:</p>
  
  <blockquote>
    <p>TypeError: Argument 1 passed to App/Doombot::__construct() must be of the type integer, 
    array given, called in /FactoryBuilder.php on line 292</p>
  </blockquote>
  
  <p>But if I comment out my models constructor my <code>make()</code> calls produce the expected results:</p>
  
  <pre><code>App\Doombot {#2942
       model_id: 2,
       weapon_id: 2,
       name: "Dannie",
     }
  </code></pre>
  
  <p>What makes sense to me is that the DoombotsFactory is trying to create a Doombot when returning, without using the Service Provider, just by using values within the array. So the objects actual constructor call occurs on <code>return [&lt;array_values&gt;]</code>? Is this line of thinking correct?</p>
  
  <p>But is there any way to have it use the service provider so that the proper parameters are already set for each newly created dummy record? </p>
  
  <p>Essentially, I'd like to just be using faker to produce a name: (Removing the <code>'&lt;field_name&gt;' =&gt; $faker-&gt;numberBetween(1,2)</code> nodes didn't help.)</p>
  
  <p><strong>Hypothetical DoombotsFactory.php</strong></p>
  
  <pre><code>$factory-&gt;define(Doombot::class, function (Faker $faker) {
  
      // create doombot object with the parameters set upon creation
      $obj = new Doombot;   
  
      // Since model_id and weapon_id have been set, add the name property
      $obj-&gt;name =&gt; $faker-&gt;firstName
  
      // somehow return this, but I believe only arrays are allowed
      return $obj;
  
  });
  </code></pre>