Laravel 工厂状态 - 无法找到工厂
Laravel Factory State- Unable to locate factory
我在执行工厂时遇到问题,我已经为工厂使用了工厂状态,但是当我执行工厂时它会给我一个错误
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 开箱即用的用户模型上,请注意,除了模型属性外,我没有更改用户模型上的任何内容。
播种机产生错误
如错误所述,您需要一个默认工厂。请比较以下两个:
$factory->define(App\User::class, function (Faker\Generator $faker) {
return [
'name' => $faker->name,
'email' => $faker->safeEmail,
'password' => bcrypt(str_random(10)),
'remember_token' => $faker->randomNumber(),
];
});
$factory->state(App\User::class, 'test_state', function (Faker\Generator $faker) {
return [
'name' => 'Namoshek',
'email' => 'namoshek@example.com',
];
});
第一个定义是用户的默认工厂,当不给出状态时。您可以使用 factory(App\User::class, 10)->create()
调用它们,其中 10
是可选的,以提供要创建的模型数量。
您还可以在调用 factory()
之后链接 ->states('test_state')
:
factory(App\User::class)->states('test_state')->create()
,它将首先 运行 默认工厂,然后将给定状态定义的更改应用到模型上。但是你总是需要一个默认工厂,否则系统不知道在哪里和什么应用状态。
顺便说一下,->create()
和->make()
是有区别的。后者只创建模型而不将它们持久保存在数据库中,而第一个模型将它们持久保存。所以 ->create()
等同于 ->make()->save()
.
有时工厂在 web 路由和测试中工作正常,但在 tinker 中它的行为与上面提到的一样。在这种情况下,您可以尝试清除 laravel 应用程序缓存。这是命令。
php artisan cache:clear
php artisan config:clear
php artisan route:clear
这将清除所有缓存。然后我可以使用工厂创建模型实例。
>>> factory(User::class)->create() // or
>>> factory(Book::class)->create()
我在执行工厂时遇到问题,我已经为工厂使用了工厂状态,但是当我执行工厂时它会给我一个错误 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 开箱即用的用户模型上,请注意,除了模型属性外,我没有更改用户模型上的任何内容。
播种机产生错误
如错误所述,您需要一个默认工厂。请比较以下两个:
$factory->define(App\User::class, function (Faker\Generator $faker) {
return [
'name' => $faker->name,
'email' => $faker->safeEmail,
'password' => bcrypt(str_random(10)),
'remember_token' => $faker->randomNumber(),
];
});
$factory->state(App\User::class, 'test_state', function (Faker\Generator $faker) {
return [
'name' => 'Namoshek',
'email' => 'namoshek@example.com',
];
});
第一个定义是用户的默认工厂,当不给出状态时。您可以使用 factory(App\User::class, 10)->create()
调用它们,其中 10
是可选的,以提供要创建的模型数量。
您还可以在调用 factory()
之后链接 ->states('test_state')
:
factory(App\User::class)->states('test_state')->create()
,它将首先 运行 默认工厂,然后将给定状态定义的更改应用到模型上。但是你总是需要一个默认工厂,否则系统不知道在哪里和什么应用状态。
顺便说一下,->create()
和->make()
是有区别的。后者只创建模型而不将它们持久保存在数据库中,而第一个模型将它们持久保存。所以 ->create()
等同于 ->make()->save()
.
有时工厂在 web 路由和测试中工作正常,但在 tinker 中它的行为与上面提到的一样。在这种情况下,您可以尝试清除 laravel 应用程序缓存。这是命令。
php artisan cache:clear
php artisan config:clear
php artisan route:clear
这将清除所有缓存。然后我可以使用工厂创建模型实例。
>>> factory(User::class)->create() // or
>>> factory(Book::class)->create()