【问题标题】:Seeding With TestDummy and Faker使用 TestDummy 和 Faker 播种
【发布时间】:2015-06-02 01:00:07
【问题描述】:

我正试图找出我做错了什么和/或处理这种情况的更好方法。

目标:

  • 能够将自己插入为用户并拥有 200 个其他用户 插入到数据库中。
  • 将我自己的静态数据插入到一个单独的配置文件表中,并将所有其他用户的随机数据插入到数据库表中。
  • 我想知道为什么我不断收到 [InvalidArgumentException] 在控制台中运行我的种子文件时出现未知格式化程序“密码”错误消息。

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')
]);

【问题讨论】:

    标签: php laravel laravel-5 faker


    【解决方案1】:

    当您从 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);  
    

    【讨论】:

    • 我确实像这样在用户模型中散列我的密码: public function setPasswordAttribute($value) { $this->attributes['password'] = \Hash::make($value); }
    • 现在唯一的问题是这并没有解决我的目标 1 和 2 的问题。
    • OmeCoatl 你有什么想法吗?
    • 在使用 $faker->word 实现变通方法后,播种器会向您抛出任何错误?您用于实现 1 和 2 的逻辑在我看来非常简单,并且必须有效。
    • 那我一定是做错了,因为我没有得到任何想要的结果。让我在上面创建一个新的堆栈问题并重新发布。
    猜你喜欢
    • 1970-01-01
    • 2018-09-03
    • 2023-03-03
    • 2017-08-29
    • 2021-02-11
    • 2017-10-02
    • 2019-05-18
    • 2019-10-25
    • 2018-09-10
    相关资源
    最近更新 更多