Error while running db: seed command

3

I'm studying laravel in version 5.2 and running the php artisan command 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);
}
}
    
asked by anonymous 24.03.2016 / 03:19

1 answer

0

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

 DB::table('users')->insert([
            'name'              => 'Admin da Silva',
            'email'             => '[email protected]',
            'password'          => bcrypt(123456),
        ]);
Now, if you use factory together with fake , you have to define the templates in the ModelFactory.php file, which would look 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 factory create a seeder :

php artisan make:seeder TableNameSeeder

And call it like this:

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

The number 10 is the number of records that should be entered into the database.

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

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

I think that's it!

I hope I have helped!

    
24.03.2016 / 14:53