目标类别[DatabaseSeeder]不存在-Laravel 7和Composer 2 [英] Target class [DatabaseSeeder] does not exist - Laravel 7 and Composer 2

查看:29
本文介绍了目标类别[DatabaseSeeder]不存在-Laravel 7和Composer 2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an app with laravel 7, and i upgrade composer 1.10 to composer 2.0, and i have this problem:


   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 ...),但均未成功.

I tried many solution (even for Laravel 8 ...) without success.

这是composer.json的自动加载

Here is the autoload of the composer.json

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

谢谢!

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

Here is what I just found: When I add in the composer.json :

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

并且我修改了SeedCommand.php的第84行"src/vendor/laravel/framework/src/Illuminate/Database/Console/Seeds/SeedCommand.php",它可以正常工作!

and that I modify line 84 of SeedCommand.php 'src/vendor/laravel/framework/src/Illuminate/Database/Console/Seeds/SeedCommand.php', it work !!!

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

作者

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

但这绝对是无法维持的...所以问题是,设置DatabaseSeeder时出现问题.

But this is absolutely not maintainable... So the problem is, there is a problem when DatabaseSeeder is set.

6个主意?

推荐答案

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

I found the solution: Laravel documention : https://laravel.com/docs/7.x/seeding#introduction

所有种子类都存储在database/seeds目录中.默认情况下,为您定义了DatabaseSeeder类.

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配合使用.

This folder is not compatible with the PSR-4, so you have to use the classmap in the composer.json with Laravel 7.

因此,还必须删除种子文件中的名称空间.

It is therefore also necessary to remove the namespaces in the files of seeds.

这是我的作曲家json:

Here is my composer json:

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

这篇关于目标类别[DatabaseSeeder]不存在-Laravel 7和Composer 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆