So I’ve been creating a CMS with Laravel and when I run this command to migrate the tables
JavaScript
x
php artisan migrate:fresh --seed
I’ve been experiencing this error
JavaScript
Dropped all tables successfully.
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated: 2014_10_12_000000_create_users_table (70.61ms)
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated: 2014_10_12_100000_create_password_resets_table (96.25ms)
Migrating: 2019_08_19_000000_create_failed_jobs_table
Migrated: 2019_08_19_000000_create_failed_jobs_table (96.25ms)
Migrating: 2021_06_07_163133_create_roles_table
Migrated: 2021_06_07_163133_create_roles_table (40.94ms)
Migrating: 2021_06_07_163453_create_role_user_table
Migrated: 2021_06_07_163453_create_role_user_table (42.74ms)
IlluminateContractsContainerBindingResolutionException
Target class [DatabaseSeedersRoleTableSeeder] does not exist.
at C:UsersbdhefactivecodeprojectblogbearvendorlaravelframeworksrcIlluminateContainerContainer.php:838
834▕
835▕ try {
836▕ $reflector = new ReflectionClass($concrete);
837▕ } catch (ReflectionException $e) {
➜ 838▕ throw new BindingResolutionException("Target class [$concrete] does not exist.", 0, $e);
839▕ }
840▕
841▕ // If the type is not instantiable, the developer is attempting to resolve
842▕ // an abstract type such as an Interface or Abstract Class and there is
1 C:UsersbdhefactivecodeprojectblogbearvendorlaravelframeworksrcIlluminateContainerContainer.php:836
ReflectionException::("Class "DatabaseSeedersRoleTableSeeder" does not exist")
2 C:UsersbdhefactivecodeprojectblogbearvendorlaravelframeworksrcIlluminateContainerContainer.php:836
ReflectionClass::__construct("DatabaseSeedersRoleTableSeeder")
I’ve tried searching to see if someone has the same problem as me, but there seem to be no similar problems. I also can’t tell if the migrations went through or not.
EDIT: Here is the seeder class
JavaScript
<?php
use AppModelsRole;
use IlluminateDatabaseSeeder;
class RoleTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$role_regular_user = new Role;
$role_regular_user->name = 'user';
$role_regular_user->description = 'A regular user';
$role_regular_user->save();
$role_admin_user = new Role;
$role_admin_user->name = 'admin';
$role_admin_user->description = 'An admin user';
$role_admin_user->save();
}
}
Here is the project structure
JavaScript
app
-> Console
-> Exceptions
-> Http
-> Models
-> Role.php
-> Providers
database
-> Factories
-> Migrations
-> Seeders
RoleTableSeeder.php
Advertisement
Answer
From the docs https://laravel.com/docs/8.x/upgrade#seeder-factory-namespaces
JavaScript
<?php
// ð••• add this line
namespace DatabaseSeeders;
use AppModelsRole;
use IlluminateDatabaseSeeder;
class RoleTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$role_regular_user = new Role;
$role_regular_user->name = 'user';
$role_regular_user->description = 'A regular user';
$role_regular_user->save();
$role_admin_user = new Role;
$role_admin_user->name = 'admin';
$role_admin_user->description = 'An admin user';
$role_admin_user->save();
}
}
Also just as a Laravel TIP
Instead of
JavaScript
$role_regular_user = new Role;
$role_regular_user->name = 'user';
$role_regular_user->description = 'A regular user';
$role_regular_user->save();
Consider using
JavaScript
$role_regular_user = Role::create([
'name' => 'user',
'description' => 'A regular user',
]);
It’s much cleaner
Don’t forget to add $fillable
property in you Role
model