问题描述
我在工匠命令中创建了一个模型工厂:
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
我与特使进行部署。
我的工厂在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]的所有答案,但没有一个起作用。
解决方法
请注意:
Unable to locate factory with name [100]
看来factory()
愿意使用状态而不是数量。在这种情况下,它正在寻找名为(string)“ 100”的工厂状态,而不是(int) 100
将您的金额变量设为整数
$numberOfPayouts = (int) $this->ask('How many payouts you want to generate?',10);
或者,尝试使用->times($amount)
方法更明确。