title: How to call a factory from a artisan command - Laravel
body: <p>I want to call a factory function from a custom artisan command. But when I run that command, it doesn't run that factory function and it also not giving any error.</p>
  
  <p>Here is custom artisan command:</p>
  
  <pre><code>namespace App\Console\Commands;
  
  use Illuminate\Console\Command;
  use App\Tag;
  
  class CreateTags extends Command
  {
      protected $signature = 'blog:create-tags';
  
      protected $description = 'To generate new random tags for blogs';
  
      public function __construct()
      {
          parent::__construct();
      }
  
      public function handle()
      {
          factory(Tag::class, 5)-&gt;create();
      }
  }
  </code></pre>
  
  <p>database/factories/Modelfactory.php:</p>
  
  <pre><code>$factory-&gt;define(App\Tag::class, function (Faker\Generator $faker) {
      return [
          'name' =&gt; $faker-&gt;word,
      ];
  });
  </code></pre>
  
  <p>To fire that command from CLI:</p>
  
  <pre><code>php artisan blog:create-tags
  </code></pre>
  
  <p>However if I run that same factory using db:seed command, its working perfectly. Like</p>
  
  <p>factories/seeds/TagsSeeder.php:</p>
  
  <pre><code>use Illuminate\Database\Seeder;
  use App\Tag;
  class TagsSeeder extends Seeder
  {
      public function run()
      {
          Tag::truncate();
          factory(Tag::class, 5)-&gt;create();
      }
  }
  </code></pre>
  
  <p>Let me know, if needs more info.</p>