Error running db:Seed command

Asked

Viewed 614 times

3

I am studying Laravel in version 5.2 and when running the command php Artisan db:Seed 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' => '[email protected]',
        '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' => '[email protected]',
        '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' => '[email protected]',
        '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);
}
}

1 answer

0


How you are setting the data directly you can use only the file Databaseseeder.php as in the documentation as follows:

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

Now, if you’re gonna use the Factory along with fake, failed to define the templates in the file Modelfactory.php, that it 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 model defined above in Factory create a seeder:

php artisan make:seeder TableNameSeeder

And call it like this:

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

Being the number 10 the number of records to be entered in 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 guess that’s it!

I hope I’ve helped!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.