Fuyu KuKie
Fuyu KuKie

Reputation: 108

unable to locate factory with name [default] [Subject]

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

Answers (3)

Tami Pangadil
Tami Pangadil

Reputation: 83

Answer by OddDream is correct.

Just to explain it more.

  1. Your model/object might not be linked properly.

    $subject = factory(\App\Subject::class)->make();

  2. 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

OddDream
OddDream

Reputation: 16

SubjectSeeder.php:

$subject = factory(\APP\Subject::class)->make();

Upvotes: -1

Related Questions