Seeding With TestDummy and Faker

2019-08-04 01:27发布

I'm trying to figure out what I'm doing wrong and/or a better way of handling this situation.

Goals:

  • To be able to insert myself as a user and also have 200 other users inserted into the database.
  • Insert static data for myself into a separate table for profiles and random data for all other users into the database table.
  • I'd like to find out why I keep getting a [InvalidArgumentException] Unknown formatter "password" error message when running my seed files in the console.

UsersTableSeeder

<?php

use Illuminate\Database\Seeder;

use Laracasts\TestDummy\Factory as TestDummy;
use App\User;

class UsersTableSeeder extends Seeder {

    public function run()
    {
        User::create([
            'first_name' => 'Secret',
            'last_name' => 'Secret',
            'username' => 'secret',
            'email' => 'secret',
            'password' => 'secret',
        ]);

        TestDummy::times(200)->create('App\User');
    }

}

UserProfilesTableSeeder.php

<?php

use Illuminate\Database\Seeder;

use Laracasts\TestDummy\Factory as TestDummy;
use App\UserProfile;

class UserProfilesTableSeeder extends Seeder {

    public function run()
    {
        UserProfile::create([
            'user_id' => '1',
            'avatar' => 'secret.jpg',
            'bio' => 'This is just my personal biography!',
            'address' => '1234 Secret Lane',
            'city' => 'Secret',
            'state' => 'Secret',
            'postcode' => '12345',
            'country' => 'United States',
            'phone' => '123-456-7890',
            'birthday' => '1988-09-04'
        ]);

        TestDummy::create('App\UserProfile');
    }

}

Factories PHP File

<?php

$factory('App\User', [
    'first_name' => $faker->firstName,
    'last_name' => $faker->lastName,
    'username' => $faker->userName,
    'email' => $faker->email,
    'password' => $faker->password
]);

$factory('App\UserProfile', [
    'user_id' => 'factory:App\User',
    'bio' => $faker->sentence(100),
    'avatar' => $faker->imageUrl($width = 640, $height = 480),
    'address' => $faker->optional($weight = 0.9)->address,
    'city' => $faker->optional($weight = 0.9)->city,
    'state' => $faker->optional($weight = 0.9)->state,
    'postcode' => $faker->optional($weight = 0.9)->postcode,
    'country' => $faker->optional($weight = 0.9)->country,
    'phone' => $faker->optional($weight = 0.9)->phoneNumber,
    'birthday' => $faker->optional($weight = 0.9)->dateTimeBetween('-40 years', '-18 years')
]);

1条回答
等我变得足够好
2楼-- · 2019-08-04 01:52

Its a known version problem, when you install faker from composer package. See this github thread:

A workaround for this use the word formatter:

//factories
$factory('App\User', [
   'first_name' => $faker->firstName,
   'last_name' => $faker->lastName,
   'username' => $faker->userName,
   'email' => $faker->email,
   'password' => $faker->word
]);

Also I recommend you to hash passwords like a good practice even in test data, use the hash method:

App::make('hash')->make($faker->word);  
查看更多
登录 后发表回答