使用 TestDummy 和 Faker 进行播种
Seeding With TestDummy and Faker
我正在尝试找出我做错了什么and/or 一种更好的处理这种情况的方法。
目标:
- 能够将我自己作为用户插入并且还有 200 个其他用户
插入数据库。
- 将我自己的静态数据插入单独的 table 个人资料,并将所有其他用户的随机数据插入数据库 table。
- 我想知道为什么我总是收到 [InvalidArgumentException]
运行 我在控制台中的种子文件时出现未知格式化程序 "password" 错误消息。
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');
}
}
工厂PHP文件
<?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')
]);
这是一个已知的版本问题,当你从 composer 包安装 faker 时。见 this github thread:
解决此问题的方法是使用单词格式化程序:
//factories
$factory('App\User', [
'first_name' => $faker->firstName,
'last_name' => $faker->lastName,
'username' => $faker->userName,
'email' => $faker->email,
'password' => $faker->word
]);
此外,我建议您对密码进行哈希处理,即使在测试数据中也是一种很好的做法,使用哈希方法:
App::make('hash')->make($faker->word);
我正在尝试找出我做错了什么and/or 一种更好的处理这种情况的方法。
目标:
- 能够将我自己作为用户插入并且还有 200 个其他用户 插入数据库。
- 将我自己的静态数据插入单独的 table 个人资料,并将所有其他用户的随机数据插入数据库 table。
- 我想知道为什么我总是收到 [InvalidArgumentException] 运行 我在控制台中的种子文件时出现未知格式化程序 "password" 错误消息。
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');
}
}
工厂PHP文件
<?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')
]);
这是一个已知的版本问题,当你从 composer 包安装 faker 时。见 this github thread:
解决此问题的方法是使用单词格式化程序:
//factories
$factory('App\User', [
'first_name' => $faker->firstName,
'last_name' => $faker->lastName,
'username' => $faker->userName,
'email' => $faker->email,
'password' => $faker->word
]);
此外,我建议您对密码进行哈希处理,即使在测试数据中也是一种很好的做法,使用哈希方法:
App::make('hash')->make($faker->word);