从下面的代码中,顶部的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 ]; });
.
解决方法
尝试
$threads->each(function ($thread) { factory(App\Reply::class,10)->create(['thread_id' => $thread->id]); });