【问题标题】:Laravel factory unable to locate factory with name [default]Laravel 工厂无法找到具有名称的工厂 [默认]
【发布时间】:2018-02-14 23:50:12
【问题描述】:

从下面的代码中,顶部的 2 个工厂正常工作,只是最后一个给出了以下错误:

InvalidArgumentException with message 'Unable to locate factory with    name [default] [App\Reply].'

输入此命令后,控制台显示错误:

$threads->each(function ($thread) { factory('App\Reply', 10)->create(['thread_id' => $thread->id]); });

我已经阅读了另一篇标题相似的帖子,但这里的情况似乎并非如此。

Laravel 5.2: Unable to locate factory with name [default]

$factory->define(App\User::class, function (Faker $faker) {
    static $password;

return [
    'name' => $faker->name,
    'email' => $faker->unique()->safeEmail,
    'password' => $password ?: $password = bcrypt('secret'),
    'remember_token' => str_random(10),
    ];
});

$factory->define(App\Thread::class, function($faker){
    return [
    'user_id' => function () {
        return factory('App\User')->create()->id;
    },
    'title' => $faker->sentence,
    'body' => $faker->paragraph
];
});

$factory->define(App\Reply::class, function($faker){
return [
    'thread_id' => function() {
        return factory('App\Thread')->create()->id;
    },
    'user_id' => function () {
        return factory('App\User')->create()->id;
    },
    'body' => $faker->paragraph
];
});

.

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    试试

    $threads->each(function ($thread) { factory(App\Reply::class, 10)->create(['thread_id' => $thread->id]); });
    

    【讨论】:

    • $threads = factory('App\Thread', 50)->create();这行得通,所以不要认为问题是''​​
    【解决方案2】:

    将这些添加到工厂代码之前的文件顶部

    use App\User;
    use App\Thread;
    use App\Reply;
    

    【讨论】:

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