3

From the below code, the 2 factory's on the top do work, just the last one gives the following error:

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

The error shows in the console after putting in this command:

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

I have read the other post with a similar title, but that doesn't seem to be the case here.

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
];
});

.

Nick Audenaerde
  • 967
  • 2
  • 14
  • 33

2 Answers2

0

try

$threads->each(function ($thread) { factory(App\Reply::class, 10)->create(['thread_id' => $thread->id]); });
Quezler
  • 2,376
  • 14
  • 29
0

Add these to top of the file before factory code

use App\User;
use App\Thread;
use App\Reply;
Abbas Palash
  • 560
  • 1
  • 6
  • 7