不能在 Laravel 8 中使用遗留工厂
Posted
技术标签:
【中文标题】不能在 Laravel 8 中使用遗留工厂【英文标题】:Cannot use legacy factories in Laravel 8 【发布时间】:2021-01-23 15:53:08 【问题描述】:我正在将我的 laravel 7.x 项目升级到 8。
我现在坚持使用遗留工厂,因为我有很多它们并且在测试和其他事情中使用它们,所以要花很长时间才能换成新的类结构。
问题是我似乎无法在本地运行我的播种机或测试(有旧工厂)。
我已导入新的 laravel/legacy-factories
并在我的播种机上设置命名空间并更新了 composer.json 文件。但是每次我尝试运行播种机或测试时都会出错
Unable to locate factory for [App\Models\User] (or any other model)
按照文档的建议,我从composer.json
中删除了classmap
,但在有和没有的情况下都进行了尝试。
我尝试完全删除所有缓存并毫无乐趣地运行composer install
。
其他人有这个问题吗?
更新 - 这是我的 composer.json 文件。一些行已被删除。
"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"
]
【问题讨论】:
你能发布你的composer.json
吗?
@Jerodev 按要求添加。
【参考方案1】:
我最终设法解决了这个问题,只需从引导目录中删除 packages.php
和 services.php
文件,然后 composer dump-autoload
就成功了。
【讨论】:
以上是关于不能在 Laravel 8 中使用遗留工厂的主要内容,如果未能解决你的问题,请参考以下文章