Reputation: 108
I just want some dummy data that I can loop out from my database but when I try to using the seeder I get this error:
unable to locate factory with name [default] [Subject]
The code I run is:
php artisan db:seed --class=SubjectSeeder
ModelFactory.php:
$factory->define(App\Subject::class, function (Faker\Generator $faker) {
return [
'name' => $faker->name,
'code' => $faker->str_random(3),
];
});
SubjectSeeder.php:
public function run()
{
$subject = factory(Subject::class)->make();
Product::create([
'name' => $subject->name,
'code' => $subject->code
]);
}
subject.php:
class Subject extends Model
{
protected $fillable = [
'name', 'code',
];
};
Upvotes: 1
Views: 2284
Reputation: 83
Answer by OddDream is correct.
Just to explain it more.
Your model/object might not be linked properly.
$subject = factory(\App\Subject::class)->make();
Another reason is the cache. You might want to do something like.
php artisan config:clear
php artisan config:cache
Let me know if it helps you.
:)
Upvotes: 1
Reputation: 1420
If someone makes the same mistake as me, i tried just to define state
factories, without default one:
$factory->define(\App\Model::class, function (Faker $faker) {
Upvotes: 1
Reputation: 16
SubjectSeeder.php:
$subject = factory(\APP\Subject::class)->make();
Upvotes: -1