Cannot use legacy factories in Laravel 8

Viewed 1516

I am upgrading my laravel 7.x project to 8.

I am sticking with the legacy factories for now as I have lots of them and have them in use in tests and things so would take forever to swap out to the new class structure.

The problem is I seem unable to be able to run my seeders or tests locally (which have legacy factories).

I have imported the new laravel/legacy-factories and set the namespaces on my seeders and updated the composer.json file. But every time I try and run a seeder or test I get an error

Unable to locate factory for [App\Models\User] (or any other model)

I removed the classmap from the composer.json as the docs suggest but tried it with and without that in place.

Ive tried completely deleting all the caches and running composer install with no joy.

Anyone else had this problem?

UPDATE - Here is my composer.json file. Some of the lines have been removed.

{
    "require": {
        "php": ">=7.4",
        "bacon/bacon-qr-code": "^2.0.2",
        "browner12/helpers": "^3.2",
        "dg/mysql-dump": "^1.5",
        "doctrine/dbal": "^2.11",
        "fzaninotto/faker": "~1.9",
        "graham-campbell/github": "^9.4.0",
        "graylog2/gelf-php": "^1.6",
        "guzzlehttp/guzzle": "^6.5.5",
        "jeremykendall/php-domain-parser": "^5.7",
        "laravel/framework": "^8.0",
        "laravel/helpers": "^1.3",
        "laravel/legacy-factories": "^1.0",
        "laravel/scout": "^8.3",
        "laravel/tinker": "^2.4",
        "laravel/ui": "^3.0",
        "league/csv": "^9.6",
        "mandrill/mandrill": "^1.0",
        "mariuzzo/laravel-js-localization": "^1.7",
        "php-http/guzzle6-adapter": "^2.0.1",
        "phpseclib/phpseclib": "^2.0.29",
        "pragmarx/google2fa": "^8.0",
        "predis/predis": "^1.1",
        "segmentio/analytics-php": "^1.5",
        "spatie/fractalistic": "^2.9",
        "zachleigh/laravel-property-bag": "^1.4"
    },
    "require-dev": {
        "laravel/dusk": "^6.8",
        "mockery/mockery": "1.4.2",
        "phpunit/phpunit": "^9.0",
        "filp/whoops": "~2.7"
    },
    "autoload": {
        // also tried with the classmap here
        "psr-4": {
            "Database\\Seeders\\": "database/seeders/",
            // Also tried with the factories namespace here
        },
        "files": [
            "app/helpers.php"
        ]
    },
    "scripts": {
        "post-root-package-install": [
            "php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "php artisan key:generate"
        ],
        "post-install-cmd": [
            "Illuminate\\Foundation\\ComposerScripts::postInstall",
            "php artisan optimize"
        ],
        "post-update-cmd": [
            "Illuminate\\Foundation\\ComposerScripts::postUpdate",
            "php artisan optimize"
        ],
        "post-autoload-dump": [
            "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
            "@php artisan package:discover"
        ]
    },
    "config": {
        "preferred-install": "dist",
        "sort-packages": true,
        "optimize-autoloader": true
    },
    "extra": {
        "laravel": {
            "dont-discover": [
                "laravel/dusk"
            ]
        }
    }
}
1 Answers

I managed to solve this in the end by simply deleting the packages.php and services.php files from the bootstrap directory and then composer dump-autoload did the trick.

Related