【发布时间】:2018-06-28 02:10:40
【问题描述】:
我在执行工厂的时候遇到了问题,我已经为工厂使用了工厂状态但是当我执行工厂时它会给我一个错误 https://laravel.com/docs/5.6/database-testing#factory-states
我有这个包含以下代码的 UserFactory.php。
<?php
use Faker\Generator as Faker;
$factory->state(App\User::class,'suggestor', function (Faker $faker) {
return [
'FirstName'=>$faker->firstName,
'LastName'=>$faker->lastName,
'Username'=>$faker->userName,
'password'=>bcrypt('123asd!@#'),
'Email'=>$faker->email,
'AccountType'=>0,
];
});
我正在使用 tinker 执行工厂命令并尝试了不同的语法,但它确实没有解决问题。
>>> factory(User::class, 1)->states('suggestor')->make();
[!] Aliasing 'User' to 'App\User' for this Tinker session.
InvalidArgumentException with message 'Unable to locate factory with name [default] [User].'
>>> factory(App\User::class, 1)->states('suggestor')->make();
InvalidArgumentException with message 'Unable to locate factory with name [default] [App/User].'
>>> factory(\App\User::class, 1)->states('suggestor')->make();
InvalidArgumentException with message 'Unable to locate factory with name [default] [App/User].'
>>> factory('App\User')->states('suggestor')->make();
InvalidArgumentException with message 'Unable to locate factory with name [default] [App/User].'
>>> factory('App\User',1)->states('suggestor')->make();
InvalidArgumentException with message 'Unable to locate factory with name [default] [App/User].'
>>>
我希望有人可以帮助我。
更新: 我试过在数据库种子上运行它,但我认为它仍然是同样的错误。 但是当我尝试其他模型时,它似乎很好。我认为问题出在 laravel 开箱即用的用户模型上,请注意,除了模型属性之外,我没有更改用户模型上的任何内容。
【问题讨论】:
-
在播种机中试一试
-
我试过在播种机中运行它,但错误是一样的。我在上面有一个关于错误的更新
-
你试过
'App\User'而不是App\User::class吗? -
是的,我也尝试过,但它不会工作
-
我想我现在明白了,我们需要先为用户模型定义一个工厂,然后才能设置任何状态。