0

I create a factory of a model inside an artisan command:

public function handle()
    {
        if (!$this->isDevelopment()) {
            $this->errorMessageSwitchEnvToDev();
            return;
        }

        $userId          = $this->ask('Please specifiy user_id you want to add the payouts to.',2148);
        $numberOfPayouts = $this->ask('How many payouts you want to generate?', 10);

        factory(\App\Payout::class, $numberOfPayouts)->create([
            'user_id' => $userId,
        ]);
    }

The artisan works on my local desktop, but it does not work after deployment on my test server.

I get the following error message:

InvalidArgumentException  : Unable to locate factory with name [100] [App\Payout].

  at /www/htdocs/w0146a6f/dev/dev4.partner.healyworld.net/releases/20201014150056/vendor/laravel/framework/src/Illuminate/Database/Eloquent/FactoryBuilder.php:269
    265|      */
    266|     protected function getRawAttributes(array $attributes = [])
    267|     {
    268|         if (! isset($this->definitions[$this->class][$this->name])) {
  > 269|             throw new InvalidArgumentException("Unable to locate factory with name [{$this->name}] [{$this->class}].");
    270|         }
    271| 
    272|         $definition = call_user_func(
    273|             $this->definitions[$this->class][$this->name],

  Exception trace:

  1   Illuminate\Database\Eloquent\FactoryBuilder::getRawAttributes([])
      /www/htdocs/w0146a6f/dev/dev4.partner.healyworld.net/releases/20201014150056/vendor/laravel/framework/src/Illuminate/Database/Eloquent/FactoryBuilder.php:292

  2   Illuminate\Database\Eloquent\FactoryBuilder::Illuminate\Database\Eloquent\{closure}()
      /www/htdocs/w0146a6f/dev/dev4.partner.healyworld.net/releases/20201014150056/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/GuardsAttributes.php:122

I do the deployment with envoyer.

My factory is defined in database/factories/PayoutFactory.php

<?php

$factory->define(\App\Payout::class, function (Faker\Generator $faker) {


    return [
        'user_id' => function () {
            return factory(App\User::class)->create()->id;
        },
        'amount' => $faker->randomFloat(2),
        'req_amount' => 0,
        'tax_amount' => 0,
        'withheld' => 0,
        'vat_rate' => $faker->randomNumber(2),
        'released_amount' => $faker->randomFloat(2),
        'released_amount_local_currency' => $faker->randomFloat(2),
        'status' => 'released',
        'flag' => 0,
        'created_at' => $faker->dateTimeBetween('-6 months', 'now'),
    ];
});

However, it won't work on production. I already cleared the cache, the routes and called composer dump-autoload, but it still failes with the same issue.

Any suggestions?

I also read all answers of Laravel 5.2: Unable to locate factory with name [default] but none of them worked.

Adam
  • 25,960
  • 22
  • 158
  • 247

1 Answers1

1

Notice this:

Unable to locate factory with name [100]

It looks like factory() is willing to use states instead of quantity. In this case it's looking for a factory state called (string) "100" instead of (int) 100

Cast your amount variable to be an integer

    $numberOfPayouts = (int) $this->ask('How many payouts you want to generate?', 10);

Alternatively, try using ->times($amount) method to be more explicit.

alariva
  • 2,051
  • 1
  • 22
  • 37