Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting Illuminate\Contracts\Container\BindingResolutionException : Target class [AdminsTableSeeder] does not exist. error

Tags:

laravel

I am creating a seeder in laravel 6.1 but I keep getting this error

Illuminate\Contracts\Container\BindingResolutionException : Target class [AdminsTableSeeder] does not exist.

I tried running composer dump-autoload and composer dumpautoload, it doesn't work for me.

here is my AdminsTableSeeder.php

use App\Models\Admin;
use Faker\Factory as Faker;
use Illuminate\Database\Seeder;

class AdminsTableSeeder extends Seeder
{
/**
 * Run the database seeds.
 *
 * @return void
 */
public function run()
{
    $faker = Faker::create();

    Admin::create([
        'name'      =>  $faker->name,
        'email'     =>  '[email protected]',
        'password'  =>  bcrypt('password'),
    ]);
}
}

and here is my DatabaseSeeder.php <?php

 use Illuminate\Database\Seeder;

 class DatabaseSeeder extends Seeder
 {
/**
 * Seed the application's database.
 *
 * @return void
 */
public function run()
{
   $this->call(AdminsTableSeeder::class);
}
}
like image 863
Bisma Avatar asked Oct 12 '19 12:10

Bisma


4 Answers

run

composer dump-autoload

then try

php artisan db:seed

like image 95
hamzaj595 Avatar answered Sep 17 '22 02:09

hamzaj595


Make sure your AdminsTableSeeder.php file is in the same directory where you have your DatabaseSeeder.php file.

Run

composer dump-autoload

then try

php artisan db:seed

Optional.

class DatabaseSeeder extends Seeder
{
     /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run(){
        $this->call('AdminsTableSeeder');
    }
}

try with $this->call('AdminsTableSeeder'); like this.

like image 21
Dilip Hirapara Avatar answered Nov 16 '22 13:11

Dilip Hirapara


In your case, move all seeder files from previous database/seeds directory to database/seeders folder & then run composer dump-autoload.


Remember, from laravel 8 seeders and factories are namespaced

To accommodate for these changes,

[1] - Add Database\Seeders namespace to your seeder classes.

 namespace Database\Seeders;

[2] - Move all seeder files to database/seeders folder.

[3] - If you import any seeders classes in DatabaseSeeder file then remove all of them. (simply remove all lines that started with use Database\Seeders\... from DatabaseSeeder.php)

[4] - Finally run dump-autoload.

composer dump-autoload

You can now try a fresh migration with seed,

php artisan migrate:fresh --seed
like image 9
muhive Avatar answered Nov 16 '22 11:11

muhive


For my case(I use Laravel 8), I solved my problem by modifying the RouteServiceProvider.php file in App/Providers/ path. I uncommented code on line 29.

protected $namespace = 'App\\Http\\Controllers';

It worked for me.

like image 4
Ivan Avatar answered Nov 16 '22 13:11

Ivan