Laravel 中的工厂模型关系

Factory model relationships in Laravel

我有 2 个 table 名为 UsersUsers_meta。两者共享 一对一 关系。我想在播种的帮助下插入虚拟数据。我能够做到这一点,唯一让我发疯的是,我无法以 user_id 作为外键在用户和 users_meta table 之间建立关系。我尝试了几种方法,但要么用相同的 user_id 创建重复的整体,要么不断重复相同的 user_id。

我真正想要的是;例如创建 100 条记录时,在插入第一个用户记录后,应该取同一用户的 user_ID,将其添加到 users_meta table的user_id字段重复插入直到100条假记录。

如有任何帮助,我们将不胜感激

代码在:UserFactory.php

$factory->define(App\User::class, function (Faker $faker) {
static $password;

return [
    'username' => $faker->userName,
    'email' => $faker->unique()->safeEmail,
    'password' => $password ?: $password = bcrypt('secret'),
    'referral_code' => str_random(10),
    'referred_by_code' => str_random(10),
    'role'  => $faker->randomElement(['administrator', 'user', 'volunteer']),
    'remember_token' => str_random(10),
]; });

代码在:UsersMetaFactory.php

$factory->define(App\Usersmeta::class, function (Faker $faker) {

return [
    'user_id'   =>  $faker->randomElement(\App\User::pluck('id')->toArray()),
    'first_name' => $faker->firstname,
    'last_name' => $faker->lastname,
    'gender' => $faker->randomElement(['male', 'female']),
    'date_of_birth' =>  $faker->dateTimeThisCentury->format('Y-m-d'),
    'address'   =>  $faker->address,
    'city'  =>  $faker->city,
    'state' =>  $faker->state,
    'zip_code'  =>  $faker->postcode,
    'country'   =>  $faker->country,
    'cell_phone'    =>  $faker->e164PhoneNumber,
    'bitcoin_address' => str_random(16),
    'monero_address'    =>  str_random(16),
    'security_question' =>  $faker->realText($maxNbChars = 20, $indexSize = 2),
    'security_answer'   =>  $faker->realText($maxNbChars = 40, $indexSize = 2),
    'is_founder'    =>  $faker->boolean($chanceOfGettingTrue = 50),
    'status'    =>  $faker->randomElement(['active', 'inactive']),
    'terms' =>  $faker->boolean
]; });

randomElement() 方法给我随机 ID,这违反了一对一关系原则,我的应用程序崩溃了。我希望它应该从用户 table 获取 id 并将与 user_id 相同的 id 传递给 users_meta table 并继续生成假记录。

CreateUsersTable 迁移class

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('username')->unique();
        $table->string('email')->unique();
        $table->string('password');
        $table->string('referral_code')->unique();
        $table->string('referred_by_code');
        $table->enum('role', ['administrator', 'user', 'volunteer'])->nullable();
        $table->rememberToken();
        $table->timestamps();
    });
}

CreateUsersMetaTable 迁移class

public function up()
{
    Schema::create('users_meta', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('user_id')->unsigned();
        $table->foreign('user_id')->references('id')->on('users');
        $table->string('first_name');
        $table->string('last_name');
        $table->enum('gender', ['male', 'female'])->nullable();
        $table->string('date_of_birth')->nullable();
        $table->string('address')->nullable();
        $table->string('city')->nullable();
        $table->string('state')->nullable();
        $table->string('zip_code')->nullable();
        $table->string('country');
        $table->string('cell_phone');
        $table->string('bitcoin_address')->nullable();
        $table->string('monero_address')->nullable();
        $table->string('security_question');
        $table->string('security_answer');
        $table->string('is_founder')->nullable();
        $table->enum('status', ['active', 'inactive'])->nullable();
        $table->string('terms');
        $table->timestamps();
    });
}

public function down()
{
    Schema::dropIfExists('users_meta');
    Schema::enableForeignKeyConstraints();
}

你应该删除这一行:

'user_id' => $faker->randomElement(\App\User::pluck('id')->toArray()),

并在创建新模型时使用关系。这是一个修改后的例子 from the docs:

factory(App\User::class, 50)->create()->each(function ($u) {
    $u->usersmeta()->save(factory(App\Usersmeta::class)->make());
});