php – Error running db:seed command

Question:

I'm studying laravel in version 5.2 and when I run the php artisan db:seed command it returns the following error:

[InvalidArgumentException]
Unable to locate factory with name [default] [CodePub\Models\User].

<?php

use Illuminate\Database\Seeder;

class UsersRolesPermissionsSeeder extends Seeder
{
/**
 * Run the database seeds.
 *
 * @return void
 */
public function run()
{
    $user = factory(\CodePub\Models\User::class)->create([
        'name' => 'Admin da Silva',
        'email' => 'admin@admin.com',
        'password' => bcrypt(123456)
    ]);

    $roleAdmin = factory(\CodePub\Models\Role::class)->create([
        'name' => 'Admin',
        'description' => 'System Administrator'
    ]);

    $user->addRole($roleAdmin);

    $userManager = factory(\CodePub\Models\User::class)->create([
        'name' => 'Manager da Silva',
        'email' => 'manager@admin.com',
        'password' => bcrypt(123456)
    ]);

    $roleManager = factory(\CodePub\Models\Role::class)->create([
        'name' => 'Manager',
        'description' => 'System Manager'
    ]);

    $userManager->addRole($roleManager);

    $userSupervisor = factory(\CodePub\Models\User::class)->create([
        'name' => 'Supervisor da Silva',
        'email' => 'supervisor@admin.com',
        'password' => bcrypt(123456)
    ]);

    $roleSupervisor = factory(\CodePub\Models\Role::class)->create([
        'name' => 'Supervisor',
        'description' => 'System Supervisor'
    ]);

    $userSupervisor->addRole($roleSupervisor);

}
}

DatabaseSeeder.php

<?php

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
/**
 * Run the database seeds.
 *
 * @return void
 */
public function run()
{
    // $this->call(UserTableSeeder::class);
    $this->call(UsersRolesPermissionsSeeder::class);
}
}

Answer:

As you are defining the data directly, you can only use the DatabaseSeeder.php file as in the documentation as follows:

 DB::table('users')->insert([
            'name'              => 'Admin da Silva',
            'email'             => 'admin@email.com',
            'password'          => bcrypt(123456),
        ]);

Now, if you are going to use the factory together with fake , the models were not defined in the ModelFactory.php file, which would be something like this:

$factory->define(App\User::class, function (Faker\Generator $faker) {
    return [
        'name' => $faker->name,
        'email' => $faker->email,
        'password' => bcrypt(str_random(10)),
    ];
});

To call the template defined above in the factory create a seeder :

php artisan make:seeder TableNameSeeder

And call it this way:

factory(App\User::class, 10)->create();

The number 10 being the number of records that must be inserted into the database.

And in the file that controls the seed -> DatabaseSeeder.php , call the function normally as described in the question:

$this->call(TableNameSeeder::class);

I think this is it!

Hope this helps!

Scroll to Top