Search code examples
phplaravel-5.3artisan-migrate

Unable to locate factory with name [default] [Modules\Brokerquotes\Entities\Insuree]


I definitely have a factory

$factory->define(Modules\Brokerquotes\Entities\Insuree::class,....

And in a DatabaseSeeder I have

factory("Modules\\Brokerquotes\\Entities\\Insuree", 5)->create()-> ......

But when I try to call the seeder class directly using php artisan db:seed --class=.... I get the subject error

Unable to locate factory with name [default] [Modules\Brokerquotes\Entities\Insuree].

I have tried to composer dumpautoload but still doesn't work

I just need to run some individual seeders from time to time.

What am I doing wrong here??


Solution

  • Given the namespace I assume you're using Asgard CMS and that you've put your Insuree Model Factory in your Module's factories directory. This won't work because by default the lookup for Model Factories is fixed to database_path('factories'), see the following in the Factory class:

    /**
     * Create a new factory container.
     *
     * @param  \Faker\Generator  $faker
     * @param  string|null  $pathToFactories
     * @return static
     */
    public static function construct(Faker $faker, $pathToFactories = null)
    {
        $pathToFactories = $pathToFactories ?: database_path('factories');
    
        return (new static($faker))->load($pathToFactories);
    }
    

    Move your Module Factory to your project's factories directory:

    <PROJECT>/
    ├── database/
    │   └── factories/
    │       └── InsureeFactory.php
    └── Modules/
    

    & then re-run your seeder:

    namespace Modules\Brokerquotes\Database\Seeders;
    
    use Illuminate\Database\Seeder;
    use Illuminate\Database\Eloquent\Model;
    
    class BrokerquotesDatabaseSeeder extends Seeder
    {
        /**
         * Run the database seeds.
         *
         * @return void
         */
        public function run()
        {
            Model::unguard();
    
            factory(\Modules\Brokerquotes\Entities\Insuree::class, 5)->create();
        }
    }
    

    Artisan command:

    php artisan db:seed --class=Modules\\Brokerquotes\\Database\\Seeders\\BrokerquotesDatabaseSeeder