Php 在Laravel中设置用户及其配置文件种子的问题

Php 在Laravel中设置用户及其配置文件种子的问题,php,laravel,laravel-seeding,Php,Laravel,Laravel Seeding,我有用户种子和用户配置文件种子。目前,当我对数据库进行种子设定时,我在users表中获得了150个用户,而在user_profiles表中只获得了4个用户的配置文件。现在,我知道问题出在UserProfileSeeder.php中的getUsers()函数中,该函数只返回四个用户的json_解码结果。我需要关于如何为第四个用户(normaluser3)编写“for”循环的帮助,以便在我的user\u profiles表中获得正确数量的结果(150)?感谢您的帮助。这是我的密码 UserSeeed

我有用户种子和用户配置文件种子。目前,当我对数据库进行种子设定时,我在users表中获得了150个用户,而在user_profiles表中只获得了4个用户的配置文件。现在,我知道问题出在UserProfileSeeder.php中的getUsers()函数中,该函数只返回四个用户的json_解码结果。我需要关于如何为第四个用户(normaluser3)编写“for”循环的帮助,以便在我的user\u profiles表中获得正确数量的结果(150)?感谢您的帮助。这是我的密码

UserSeeeder.php

<?php

use App\User;
use App\Gender;
use Carbon\Carbon;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;

class UserSeeder extends Seeder
{
    /**
    * Run the database seeds.
    *
    * @return void
    */
    public function run()
    {
        $genders = DB::table('genders')->insert([
            [
                'genders' => 'Woman',
            ],
            [
                'genders' => 'Woman Looking For Woman',
            ],
            [
                'genders' => 'Man',
            ]
        ]);

        $genderIds = Gender::pluck('id');

        DB::table('users')->insert([
            'gender_id' => $genderIds->random(),
            'name' => 'authuser',
            'email' => 'authuser@auth.com',
            'email_verified_at' => now(),
            'password' => Hash::make('auth123456'),
            'age' => 18,
            'premium' => rand(0, 1),
            'remember_token' => Str::random(10), 
            'created_at' => Carbon::now(),
            'updated_at' => Carbon::now(),
        ]); 

        DB::table('users')->insert([
            'gender_id' => $genderIds->random(),
            'name' => 'normaluser',
            'email' => 'normaluser@normal.com',
            'email_verified_at' => now(),
            'password' => Hash::make('normal123456'),
            'age' => 18,
            'premium' => rand(0, 1),
            'remember_token' => Str::random(10), 
            'created_at' => Carbon::now(),
            'updated_at' => Carbon::now(),
        ]);

        DB::table('users')->insert([
            'gender_id' => $genderIds->random(),
            'name' => 'normaluser2',
            'email' => 'normaluser2@normal2.com',
            'email_verified_at' => now(),
            'password' => Hash::make('normal2123456'),
            'age' => 18,
            'premium' => rand(0, 1),
            'remember_token' => Str::random(10), 
            'created_at' => Carbon::now(),
            'updated_at' => Carbon::now(),
        ]);

        $faker = Faker\Factory::create();
        for ($i = 0, $n = 150; $i < $n; $i++) {
            DB::table('users')->insert([
                [
                    'gender_id' => $genderIds->random(),
                    'name' => 'normaluser3',
                    'email' => $faker->unique()->safeEmail,
                    'email_verified_at' => now(),
                    'password' => Str::random(12),
                    'age' => 18,
                    'premium' => rand(0, 1),
                    'remember_token' => Str::random(10), 
                    'created_at' => Carbon::now(),
                    'updated_at' => Carbon::now(),
                ]
            ]);
        }
    }
}
getUsers()
替换为
User:all()
以迭代数据库中的每个用户:

foreach (User::all() as $user) {
    DB::table('user_profiles')->insert([
        'user_id' => $user->id,
        // the rest of your attributes
    ]);

    // if you have a relationship for the profile on your user model
    // this way you can omit the user_id, created_at and updated_at attributes
    $user->profile()->create([
        // the rest of your attributes
    ]);
}

您的代码和问题非常混乱,是否要为数据库中的每个用户创建配置文件?您正在迭代getUsers,它总共有4个值,而不是迭代所有用户。我建议遍历User::all,然后从getUsers数组中获取一个随机值。@Remul是的,这就是我想要的。@aynber是的,我知道你的意思。您能帮我了解一下确切的代码吗?我现在在用户配置文件表中找到了“SQLSTATE[42S22]:列未找到:1054未知列“已在“字段列表”中创建”。但我没有在迁移、数据库或播种机中创建_at,它不应该存在。为什么显示未找到列?添加
public$timestamps=FALSE到你的个人资料模型。我做到了。它现在只需做一些小改动就可以工作了。谢谢你们两位!
foreach (User::all() as $user) {
    DB::table('user_profiles')->insert([
        'user_id' => $user->id,
        // the rest of your attributes
    ]);

    // if you have a relationship for the profile on your user model
    // this way you can omit the user_id, created_at and updated_at attributes
    $user->profile()->create([
        // the rest of your attributes
    ]);
}