【问题标题】:Unable to locate factory with name on production?无法找到生产名称的工厂?
【发布时间】:2021-01-29 00:54:25
【问题描述】:

我在工匠命令中创建了一个模型工厂:

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,
        ]);
    }

工匠在我的本地桌面上工作,但在我的测试服务器上部署后无法工作。

我收到以下错误消息:

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

我与 envoyer 一起进行部署。

我的工厂定义在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'),
    ];
});

但是,它不适用于生产。我已经清除了缓存、路由并调用了composer dump-autoload,但它仍然因同样的问题而失败。

有什么建议吗?

我还阅读了Laravel 5.2: Unable to locate factory with name [default] 的所有答案,但没有一个有效。

【问题讨论】:

    标签: php laravel-5 phpunit


    【解决方案1】:

    注意这一点:

    Unable to locate factory with name [100]
    

    看起来factory() 愿意使用states 而不是quantity。在这种情况下,它正在寻找一个名为 (string) "100" 而不是 (int) 100

    的工厂状态

    将金额变量转换为整数

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

    或者,尝试使用-&gt;times($amount) 方法更明确。

    【讨论】:

      猜你喜欢
      • 2018-02-14
      • 2019-05-26
      • 2019-10-05
      • 2017-09-20
      • 2020-02-26
      • 2016-07-25
      • 2019-03-19
      • 1970-01-01
      • 2017-03-13
      相关资源
      最近更新 更多