Laravel中的工厂模型关系

Laravel中的工厂模型关系,laravel,foreign-keys,factory,faker,laravel-seeding,Laravel,Foreign Keys,Factory,Faker,Laravel Seeding,我有两个名为Users和Users\u meta的表。双方都在分享一对一的关系。我想在播种的帮助下插入虚拟数据。我能做到这一点,唯一让我抓狂的是,我无法建立用户和用户之间的关系,用户id作为外键的元表。我尝试了几种方法,但要么创建具有相同用户id的重复实体,要么不断重复相同的用户id 我真正想要的是;例如,在创建100条记录时,在第一次插入用户记录后,它应该使用同一用户的用户ID,将其添加到用户ID表的用户ID字段中,并重复插入,直到插入100条假记录 在此方面的任何帮助都将不胜感激 在:Use

我有两个名为UsersUsers\u meta的表。双方都在分享一对一的关系。我想在播种的帮助下插入虚拟数据。我能做到这一点,唯一让我抓狂的是,我无法建立用户和用户之间的关系,用户id作为外键的元表。我尝试了几种方法,但要么创建具有相同用户id的重复实体,要么不断重复相同的用户id

我真正想要的是;例如,在创建100条记录时,在第一次插入用户记录后,它应该使用同一用户的用户ID,将其添加到用户ID表的用户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),
]; });
$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
]; });
代码输入:UsersMetaFactory.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),
]; });
$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,我的应用程序出现故障。我希望它应该从users表中获取id,并将与users\u id相同的id传递给users\u meta表,并继续生成假记录

CreateUsersTable迁移类

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();
    });
}
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();
}
CreateUsersMetaTable迁移类

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();
    });
}
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()),
并在创建新模型时使用关系。下面是一个经过修改的示例:


你的解决方案完全符合我的需要。但现在我遇到了另一个问题,终端给出msg:SQLSTATE[HY000]:一般错误:1364字段“user_id”没有默认值。更新有问题的迁移表时请务必检查。@ChetanChitte我认为此代码不会导致错误,因为
$u->usersmeta()
正在设置
用户id
。请检查导致错误的确切代码。您的回答确实解决了问题。这可能是另一个问题。这是唯一一个抛出这个错误的终端,其余的看起来都工作得很好。很高兴它有帮助。