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);
}
}
run
composer dump-autoload
then try
php artisan db:seed
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.
In your case, move all seeder files from previous database/seeds
directory to database/seeders
folder & then run composer dump-autoload
.
laravel 8
seeders and factories are namespacedTo 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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With