【问题标题】:Laravel Factory State- Unable to locate factoryLaravel 工厂状态 - 无法找到工厂
【发布时间】: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吗?
  • 是的,我也尝试过,但它不会工作
  • 我想我现在明白了,我们需要先为用户模型定义一个工厂,然后才能设置任何状态。

标签: php laravel state factory


【解决方案1】:

如错误所述,您需要一个默认工厂。请比较以下两个:

$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)-&gt;create() 调用它们,其中10 是可选的,以提供要创建的模型数量。

您还可以在调用factory() 之后链接-&gt;states('test_state'): factory(App\User::class)-&gt;states('test_state')-&gt;create(),它将首先运行默认工厂,然后在模型上应用由给定状态定义的更改。但是你总是需要一个默认工厂,否则系统不知道将状态应用到哪里以及应用到什么。

顺便说一句,-&gt;create() 和 -&gt;make() 是有区别的。后者只创建模型而不将它们持久化到数据库中,而第一个则持久化它们。所以-&gt;create() 等价于-&gt;make()-&gt;save()。

【讨论】:

    【解决方案2】:

    有时工厂在网络路由和测试中也可以正常工作,但在修补程序中它的行为如上所述。在这种情况下,您可以尝试清除 laravel 应用程序缓存。这里是命令。

    php artisan cache:clear 
    php artisan config:clear
    php artisan route:clear
    

    这将清除所有缓存。然后我可以使用工厂创建模型实例。

    >>> factory(User::class)->create() // or
    >>> factory(Book::class)->create()
    

    【讨论】:

      猜你喜欢
      • 2019-10-05
      • 2018-02-14
      • 2020-10-04
      • 2020-11-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-11
      • 2023-02-04
      • 2020-11-20
      相关资源
      最近更新 更多