Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.2: Unable to locate factory with name [default]

I want to seed database when I use this

 public function run()
{
    $users = factory(app\User::class, 3)->create();
}

Add three user in database but when I use this

 public function run()
{
    $Comment= factory(app\Comment::class, 3)->create();
}

Show me error

[InvalidArgumentException]
Unable to locate factory with name [default] [app\Comment].

like image 802
paranoid Avatar asked Apr 05 '16 08:04

paranoid


7 Answers

Some times it could be due to importing the wrong TestCase

use PHPUnit\Framework\TestCase; [WRONG: and throws this error]


use Tests\TestCase; [CORRECT]

like image 78
Ayeni TonyOpe Avatar answered Nov 12 '22 11:11

Ayeni TonyOpe


If nothing helps with PHPUnit.

For those of readers who stuck with the same issue in tests, I found out that I forgot to add parent::setUp() in setUp method.

like image 89
Darmen Amanbayev Avatar answered Nov 12 '22 09:11

Darmen Amanbayev


By default the laravel installation comes with this code in the database/factories/ModelFactory.php File.

$factory->define(App\User::class, function (Faker\Generator $faker) {
    return [
        'name' => $faker->name,
        'email' => $faker->email,
        'password' => bcrypt(str_random(10)),
        'remember_token' => str_random(10),
    ];
});

So you need to define a factory Model before you use it to seed database. This just uses an instance of Faker Library which is used to generate fake Data for seeding the database to perform testing.

Make sure You have added a similar Modal Factory for the Comments Model.

So your Comments Model Factory will be something like this :

$factory->define(App\Comment::class, function (Faker\Generator $faker) {
    return [
        'comment' => $faker->sentence,
         // Any other Fields in your Comments Model 
    ];
});
like image 48
Mohan Avatar answered Nov 12 '22 09:11

Mohan


This can also happen when you are running the command factory()->create() from php artisan tinker. Make sure you save the file database/factories/ModelFactory.php before opening tinker

like image 19
Agu Dondo Avatar answered Nov 12 '22 09:11

Agu Dondo


I'm using laravel 5.5 and for that doing this is bit different. You have to create CommentFactory.php inside \database\factories directory and add this inside,

$factory->define(App\Comment::class, function (Faker\Generator $faker) {
    return [
        'comment' => $faker->sentence,
         // Any other Fields in your Comments Model 
    ];
});

And add this,

$Comment= factory(\App\Comment::class, 3)->create();

instead of

$Comment= factory(app\Comment::class, 3)->create();

I just wanted to add this since I'm facing the same issue for later version and this thread helped me a lot to fix it.

like image 14
vimuth Avatar answered Nov 12 '22 10:11

vimuth


  1. Step -   Make sure CommentFactory is using Comment instead of Model.

    use App\Comment ...

    $factory->define(Comment::class, function (Faker $faker){
    
  2. Step - Verify that the names are correct in CommentsTableSeeder.

    use App\Comment ...

    public function run() { factory(Comment::class, 3)->create(); }

Good luck!

like image 12
Cherma Ramalho Avatar answered Nov 12 '22 09:11

Cherma Ramalho


if you had this issue while using a unit test, this may solve your problem. Extends your class with

Tests\TestCase

instead of

PHPUnit\Framework\TestCase

like image 12
M.abdelrhman Avatar answered Nov 12 '22 10:11

M.abdelrhman