Laravel工厂无法找到名称[默认]的工厂

时间:2017-09-06 13:04:33

标签: php laravel

从下面的代码中,顶部的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
];
});

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

将它们添加到工厂代码之前的文件顶部

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