Search code examples
phplaravelbackendfactorylaravel-8

how to run specific factory in laravel 8


I need to run factory using tinker but my model isn't in app folder like this->App\Models\User::factory()->count(2)->create();

the path is-> Modules\Menu\Entities\MenuPosition and when I run tis command

php artisan tinker
Modules\Menu\Entities\MenuPosition::factory()->count(2)->create();

there is error

Class 'Database/Factories/Modules/Menu/Entities/MenuPositionFactory' not found in C:/laragon/www/menu--test/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Factories/Factory.php on line 656

Solution

  • You can override the newFactory method on this model:

    /**
     * Create a new factory instance for the model.
     *
     * @return \Illuminate\Database\Eloquent\Factories\Factory
     */
    protected static function newFactory()
    {
        return \Modules\Menu\Database\Factories\MenuFactory::new();
    }
    

    Or you can look into the documentation to see how to adjust how the factory is looked up based on the model.