目标 class [DatabaseSeeder] 不存在 - Laravel 7 和 Composer 2

Target class [DatabaseSeeder] does not exist - Laravel 7 and Composer 2

我有一个带有 laravel 7 的应用程序,我将 composer 1.10 升级到 composer 2.0,但我遇到了这个问题:


   Illuminate\Contracts\Container\BindingResolutionException

  Target class [DatabaseSeeder] does not exist.

  at vendor/laravel/framework/src/Illuminate/Container/Container.php:807
    803|
    804|         try {
    805|             $reflector = new ReflectionClass($concrete);
    806|         } catch (ReflectionException $e) {
  > 807|             throw new BindingResolutionException("Target class [$concrete] does not exist.", 0, $e);
    808|         }
    809|
    810|         // If the type is not instantiable, the developer is attempting to resolve
    811|         // an abstract type such as an Interface or Abstract Class and there is

      +37 vendor frames
  38  artisan:37
      Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

我尝试了很多解决方案(甚至 Laravel 8 ...)都没有成功。

这是 composer.json

的自动加载
        "psr-4": {
            "App\": "app/",
            "DatabaseSeeder\": "database/seeds"
        },
        "files": [
            "app/helpers.php"
        ]
    }

谢谢!

编辑

这是我刚刚发现的:当我添加 composer.json 时:

  "autoload": {
        "psr-4": {
            "App\": "app/",
            "Database\Factories\": "database/factories",
            "Database\Seeds\": "database/seeds",
            "DatabaseSeeder\": "database/seeds/",
} },

并且我修改了 SeedCommand.php 'src/vendor/laravel/framework/src/Illuminate/Database/Console/Seeds/SeedCommand.php' 的第 84 行,它起作用了!!!

$class = $this->laravel->make($this->input->getOption('class'));

来自

$class = $this->laravel->make('Database\Seeds\DatabaseSeeder');

但这绝对不可维护... 那么问题来了,DatabaseSeeder设置的时候出现了问题

6个想法?

我找到了解决方案: Laravel 文档:https://laravel.com/docs/7.x/seeding#introduction

All seed classes are stored in the database/seeds directory.... By default, a DatabaseSeeder class is defined for you.

此文件夹与 PSR-4 不兼容,因此您必须使用 composer.json 中的类映射和 Laravel 7。

因此也有必要删除种子文件中的命名空间。

这是我的作曲家json:

"autoload": {
        "psr-4": {
            "App\": "app/",
        },
        "classmap": [
            "database/seeds",
            "database/factories"
        ],
        "files": [
            "app/helpers.php"
        ]
    }

我是这样解决的:

  • 我遵循了文档中关于 Sedeers 和工厂的内容
  • 我运行 composer dump-autoload
  • 将作曲家更新到版本 2