14
Illuminate\Contracts\Container\BindingResolutionException

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

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

  1   C:\......\blog\vendor\laravel\framework\src\Illuminate\Container\Container.php:809
      ReflectionException::("Class Database\Seeders\CountriesTableSeeder does not exist")

  2   C:\......\blog\vendor\laravel\framework\src\Illuminate\Container\Container.php:809
      ReflectionClass::__construct("Database\Seeders\CountriesTableSeeder")
Salman Zafar
  • 3,844
  • 5
  • 20
  • 43
Ali Hussain Qumar
  • 149
  • 1
  • 1
  • 3
  • It can't find your seeder called `CountriesTableSeeder`. Try `composer dump-autoload -o` and then rerun. Also you should provide some code showing us how the seeder is defined, etc. – PatricNox Sep 29 '20 at 09:25
  • i was tried the composer dump-autoload -o command , but not working. – Ali Hussain Qumar Sep 29 '20 at 09:35

10 Answers10

16

For Laravel 8 you need to make the below changes to an existing project for seeding to work:

  1. Add the Database\Seeders namespace to your DatabaseSeeder.php and other seeder files :
<?php

namespace Database\Seeders;
  1. Change the folder name of database/seeds to database/seeders.

  2. Update composer.json like below:

"autoload": {
    "psr-4": {
        "App\\": "app/",
        "Database\\Factories\\": "database/factories/",
        "Database\\Seeders\\": "database/seeders/"
    }
},
  1. Finally, run the below commands:
composer dump-autoload
php artisan db:seed
Amit Gupta
  • 2,771
  • 2
  • 17
  • 31
15

From laravel 8 Seeders and factories are now namespaced


To accommodate for these changes, add Database\Seeders namespace to your seeder classes.

namespace Database\Seeders;

In addition, move all seeder files from previous database/seeds directory to database/seeders folder.


In your case remove all lines started with use Database\Seeders\...
from DatabaseSeeder.php file

It should solve the issue,

You can also run dump-autoload & fresh migration with seed,

composer dump-autoload

php artisan migrate:fresh --seed
muhive
  • 1,879
  • 14
  • 20
1

working with laravel 8

change directory name database/seeds to database/seeders.

add namespace Database\Seeders; to seeders file.

namespace Database\Seeders;

use Illuminate\Database\Seeder;
class TableSeeder extends Seeder{
}

Update Compose.json file

"autoload": {
"psr-4": {
    "App\\": "app/",
    "Database\\Factories\\": "database/factories/",
    "Database\\Seeders\\": "database/seeders/"
}},

run in terminal

composer dump-autoload
php artisan db:seed --class=TableSeeder
Nirav Bhoi
  • 559
  • 9
  • 17
0

When working with new seeders we might get

Target class [...TableSeeder] does not exist.

In my case, simply running

composer dump-autoload

did the trick

miken32
  • 42,008
  • 16
  • 111
  • 154
Tiago Martins Peres
  • 14,289
  • 18
  • 86
  • 145
0

remove using TableSeeder at the top of the DatabaseSeeder.php file.

  • 1
    I tried all the methods in this thread but none of them worked for me. Then I remove the above mentioned line and it worked well for me. – Dulanjana_Bandara Jul 23 '21 at 05:51
-1

DatabaseSeeder.php

<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model; 

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {
        $this->call([
            CountriesTableSeeder::class,
            ProvincesTableSeeder::class,
            RegionsTableSeeder::class,
            PermisionsTableSeeder::class,
            RolesTableSeeder::class,
            StatusTypesTableSeeder::class,
            StatusesTableSeeder::class,
            ]);
    }
}

CountriesTableSeeder.php

<?php

use App\Country;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Model; 

class CountriesTableSeeder extends Seeder
{
    private $numberOfCountries = 10;

    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        DB::table('countries')->insert([
            ['country_name' => 'iraq'],
            ['country_name' => 'qater'],
        ]);
    }
}
Ali Hussain Qumar
  • 149
  • 1
  • 1
  • 3
-1

After running: composer dump:autoload

I could just include the below code in my DatabaseSeeder and It would include the data from SubsidyregimeTableSeeder

$this->call([
   SubsidyregimeTableSeeder::class
]);
Tomer Shetah
  • 8,413
  • 7
  • 27
  • 35
-1

In my case, I had an existing code of version 5.8 of Laravel. So the files were duplicated in Seeds folder, the namespace was missing in and I had to reinstall Voyager with dummy like so :

composer dump-autoload

php voyager:install --with-dummy

fixed the namespace to namespace Database\Seeders; instead of namespace Database\Seeds;

Rose Riyadh
  • 518
  • 6
  • 16
-1
  1. Rename directory database/seeds to database/seeders
  2. Open composer.json file and change on autoload section.

"autoload": { "psr-4": { "App\": "app/",

    "Database\\Factories\\": "database/factories/",

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

}

}

  1. Open Database Seeder file database/seeders/DatabaseSeeder.php and add namespace Database\Seeders; on top of page.
  2. Open your others seeders file and add namespace Database\Seeders; to all seeders class.
  3. Run composer dump-autoload and php artisan migrate:fresh --seed
oli7crown
  • 1
  • 1
-1

Remember if you upgrade ti laravel 8 you need to change directory name database/seeds to database/seeders.

Ahmed Saad
  • 53
  • 8