Framework: seeder does't work correctly if I specify --class=... option

Created on 10 Sep 2020  路  14Comments  路  Source: laravel/framework


  • Laravel Version: 8.0.0
  • PHP Version: 7.4.9
  • Database Driver & Version: pgsql&12

Description:

After upgrading to Laravel 8 from latest Laravel 7,
Seeder doesn't work if I specify --class=.. option.
Without specifying --class,
php artisan db:seed
can run.

Steps To Reproduce:

php artisan make:seeder AdditionalJobAttrSeeder;
php artisan db:seed --class=AdditionalJobAttrSeeder;
In Container.php line 811:                                                                           
  Target class [Database\Seeders\AdditionalJobAttrSeeder] does not exist.  
In Container.php line 809:                                                                 
  Class Database\Seeders\AdditionalJobAttrSeeder does not exist  

I could resolve this problem by editing
vendor/laravel/framework/src/Illuminate/Database/Console/Seeds/SeedCommand.php

by commenting out

        if (strpos($class, '\\') === false) {
        //    $class = 'Database\\Seeders\\'.$class;
        }

which means I can resolve the issue by typing

php artisan db:seed --class=\\AdditionalJobAttrSeeder;

even if I don't edit the file but this is different behavior from official document and previous behavior.

needs more info

Most helpful comment

I'm using Laravel 8.6.0, created a seeder, it includes the namespaces by default, and the files are put in the database/seeders directory, but still cant find my seeder, even after running composer dump-autoload :

   Illuminate\Contracts\Container\BindingResolutionException 

  Target class [CoursesTableSeeder] does not exist.

All 14 comments

Thanks for you comment.

compser.json and composer dump were done.
but

Did you add the new namespaces?

Seed Class was created in Laravel 7.25 by "php artisan make:seeder ..." and at the time it doesn't have the namespace but it worked.
But I noticed that if I create seed class in 8.0.0 from zero using "php artisan make:seeder TestSeeder", it now adds namespace.

So the problem is SeederClass created by lalavel 7 lost compatibility with Laravel 8 without changing the code if we specify --class=... option.

If it is described in https://laravel.com/docs/8.x/upgrade as compatibility problem, it would be better but I think the solution is clear now.

Same problem here and that's how I fixed:

As hikarine3 said would be nice to have this compatibility issue mentioned in the upgrade guide.

Thanks!

Thanks. I've sent in a PR here: https://github.com/laravel/docs/pull/6339

I have derictories into "seeders". If i write psr-4 namespase for folder, - error target class [Database\Seeders\SafeSeeder] does not exist

namespace Database\Seeders\Safe;

If I am correct,
php artisan make:seeder TestSeeder
will create the file under
database/seeds
yet even in Laravel 8.0.0, so writing

            "Database\\Seeders\\": "database/seeders/"

doesn't match with default location, I am afraid.
It should be

  "Database\\Seeders\\": "database/seeds/"

or the location of creation of "php artisan make:seeder ..." should be changed.

Hello,

I executed(With Laravel 8.0.3) php artisan make:seeder TestSeeder and it created the file under database/seeders with the namespace Database\Seeders

so it's working fine for me :)

Hmm, then I guess

grep -r seeds vendor/laravel/framework 
vendor/laravel/framework/src/Illuminate/Database/Console/Seeds/SeederMakeCommand.php:        if (is_dir($this->laravel->databasePath().'/seeds')) {
vendor/laravel/framework/src/Illuminate/Database/Console/Seeds/SeederMakeCommand.php:            return $this->laravel->databasePath().'/seeds/'.$name.'.php';

affected the difference of behavior (Laravel 8.0.4).
So if we remove "database/seeds" directory, I think the behavior of creation of class will change like you said.
So either way is O.K in code's perspective but we should set correct path in composer.json depending on existence of dastabase/seeds directory.

Yeah, I have assumed you followed my steps form here https://github.com/laravel/framework/issues/34243#issuecomment-690112896 馃槄

I haven't tried, but maybe you can change the default seeder stub and put the old seeds path in the composer.json, so then you don't need to change the directory name ( I didn't tested this!)

Either can be O.K but changing like you would be better considering it is default location in Laravel 8 though we don't see change if we haven't renamed database/seeds directory.

I'm using Laravel 8.6.0, created a seeder, it includes the namespaces by default, and the files are put in the database/seeders directory, but still cant find my seeder, even after running composer dump-autoload :

   Illuminate\Contracts\Container\BindingResolutionException 

  Target class [CoursesTableSeeder] does not exist.

sorry to zombify this thread, but I have followed the steps indicated on comment #34243 and still have the same issue described by @ekosynth and @hikarine3 on previous comments:

   Illuminate\Contracts\Container\BindingResolutionException 

  Target class [Database\Seeders\CoursesTableSeeder] does not exist.

  at vendor/laravel/framework/src/Illuminate/Container/Container.php:811

Any ideas on how to solve this?

It still not working, Laravel 8.10.0, checked everything described in this thread

UPD

Ok, I have made like this:

"autoload": {
      "psr-4": {
        "App\\": "app/",
        "Database\\Factories\\": "database/factories/",
        "Database\\Seeders\\": "database/seeders/"
      }
    },
    "psr-4": {
      "App\\": "app/",
      "Database\\Factories\\": "database/factories/",
      "Database\\Seeders\\": "database/seeders/"
    }
Was this page helpful?
0 / 5 - 0 ratings

Related issues

lzp819739483 picture lzp819739483  路  3Comments

YannPl picture YannPl  路  3Comments

gabriellimo picture gabriellimo  路  3Comments

JamborJan picture JamborJan  路  3Comments

klimentLambevski picture klimentLambevski  路  3Comments