PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, January 7, 2022

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

 January 07, 2022     composer-php, laravel-7     No comments   

Issue

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))

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

Here is the autoload of the composer.json

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

Thx !

EDIT

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/",
} },

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'));

by

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

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

6An idea ?


Solution

I found the solution: Laravel documention : 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.

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.

Here is my composer json:

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


Answered By - Benjamin GUILLOT
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing