Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

in laravel 8 with seeding , i has this issue Target class [TableSeeder] does not exist

Tags:

php

laravel

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")
like image 598
Ali Hussain Qumar Avatar asked Sep 29 '20 09:09

Ali Hussain Qumar


People also ask

Which command to run seeding in Laravel?

Laravel Seeding Creating a Seeder To create seeders, you may use the make:seeder Artisan command. All seeders generated will be placed in the database/seeds directory. Generated seeders will contain one method: run . You may insert data into your database in this method.

What is seeding Laravel 8?

Laravel includes the ability to seed your database with data using seed classes. All seed classes are stored in the database/seeders directory. By default, a DatabaseSeeder class is defined for you. From this class, you may use the call method to run other seed classes, allowing you to control the seeding order.

How do I roll back seeding in Laravel?

use Undo Seeder for Laravel. When you install UndoSeeder, the following artisan commands are made available: db:seed-undo Undo seeds in the seeds directory. db:seed-refresh Undo seeds run seeds again.

Why seeding is used in Laravel?

Laravel offers a tool to include dummy data to the database automatically. This process is called seeding. Developers can add simply testing data to their database table using the database seeder. It is extremely useful as testing with various data types allows developers to detect bugs and optimize performance.


2 Answers

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
like image 90
muhive Avatar answered Oct 23 '22 02:10

muhive


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
like image 14
Amit Gupta Avatar answered Oct 23 '22 04:10

Amit Gupta