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

PhpLaravelLaravel 5Laravel 5.2Laravel Seeding

Php Problem Overview


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

Php Solutions


Solution 1 - Php

Some times it could be due to importing the wrong TestCase

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


use Tests\TestCase; [CORRECT]

Solution 2 - Php

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.

Solution 3 - Php

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

Solution 4 - Php

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

Solution 5 - Php

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.

Solution 6 - Php

  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!

Solution 7 - Php

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

Solution 8 - Php

In my case the error was that I've imported the wrong test base class. Instead of extending Laravel's Tests\TestCase I imported the TestCase class from PHPUnit. Silly, but it took me quite some time to figure this out.

Solution 9 - Php

I'm using Laravel Framework 5.7.19. And in my case, my factory file is generated from command line make:factory. The default model in the file is:

$factory->define(Model::class, ...

You should change the name Model to the thing you exactly want to use, in my case it is \App\InterviewQuestion, so it becomes:

$factory->define(\App\InterviewQuestion::class, ...

Solution 10 - Php

This could be a cache problem. You can resolve it executing the commands following commands.

php artisan clear-compiled
composer dump-autoload
php artisan optimize

Solution 11 - Php

None of the answers above did work for me. The best way to fix this error entirely is by registering your factories in TestCase class like that:

<?php

namespace Vendo\Package\Tests;

use Vendor\Package\InboxServiceProvider;
use Illuminate\Database\Eloquent\Factory as EloquentFactory;

class TestCase extends \Orchestra\Testbench\TestCase
{
  public function setUp(): void
  {
    parent::setUp();
    // additional setup

    $this->app->make(EloquentFactory::class)->load(__DIR__ . '/../database/factories');
  }

Solution 12 - Php

I was trying to test Model Factory from tinker. I had created model factory as explained in above thread and other Laravel docs. But it would not run and threw an InvalidArgumentException with message

> Unable to locate factory with name [default] [/App/Game]

I was running it in Tinker command line as:

factory('\App\Game')->create();

After some time, I found that problem was the leading backslash \. I ran it like below and it worked fine.:

factory('App\Game')->create();

Silly thing, but may help someone.

Solution 13 - Php

none of the answers worked for me so I delete my factory and create it again (every this is same with the previous factory) and this problem solved for me

Solution 14 - Php

I had the same problem. Tried everything. Nothing worked. Then I exited Tinker and started it up again and it worked!

Solution 15 - Php

In my case, the problem arose because I was using

factory('App\schedule')->create();

Instead of

factory('App\Schedule')->create();

The first letter in "Schedule" was capitalized, so check your spelling, it could be the problem.

Solution 16 - Php

Note Few points to remember for Laravel 8+ :

Replace

use PHPUnit\Framework\TestCase;

With

use Tests\TestCase;

Add

use RefreshDatabase;

use WithFaker;

in your test class.

New way of using factories in Laravel tests is :

Post::factory()->create();

For More details see the attached screens :

PostFactory

Post Model

PostController

posts view

post test file

web routes for posts

Solution 17 - Php

In my case this issue arose because I hadn't actually made the factory I was trying to call - I had made a bunch of models at once and just forgot to create that one factory!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionparanoidView Question on Stackoverflow
Solution 1 - PhpAyeni TonyOpeView Answer on Stackoverflow
Solution 2 - PhpDarmen AmanbayevView Answer on Stackoverflow
Solution 3 - PhpMohanView Answer on Stackoverflow
Solution 4 - PhpAgu DondoView Answer on Stackoverflow
Solution 5 - PhpvimuthView Answer on Stackoverflow
Solution 6 - PhpCherma RamalhoView Answer on Stackoverflow
Solution 7 - PhpM.abdelrhmanView Answer on Stackoverflow
Solution 8 - PhpgrzeniuficationView Answer on Stackoverflow
Solution 9 - PhpYarcoView Answer on Stackoverflow
Solution 10 - PhpAbhilash AsokanView Answer on Stackoverflow
Solution 11 - Phpuser2682025View Answer on Stackoverflow
Solution 12 - PhpMihir KagranaView Answer on Stackoverflow
Solution 13 - Phpyas17View Answer on Stackoverflow
Solution 14 - Phple0li0nView Answer on Stackoverflow
Solution 15 - PhpGamopoView Answer on Stackoverflow
Solution 16 - PhpAadharView Answer on Stackoverflow
Solution 17 - PhpAbraham BrookesView Answer on Stackoverflow