在 Laravel 中播种用户及其个人资料时出现问题
Problem with seeding users and their profiles in Laravel
我有用户播种器和用户个人资料播种器。目前,当我为我的数据库播种时,我在用户 table 中得到了一百五十个用户,只有四个用户的个人资料填写在 user_profiles table 中。现在,我知道问题出在 UserProfileSeeder.php 中的 getUsers() 函数中,returns json_decode 结果只有四个用户。我需要有关如何为第四个用户 (normaluser3) 编写 'for' 循环的帮助,以便我在 user_profiles table 中获得正确数量的结果 (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(),
]
]);
}
}
}
UserProfileSeeder.php
<?php
use App\User;
use Carbon\Carbon;
use Faker\Provider\Address;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
class UserProfileSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$faker = Faker\Factory::create();
foreach ($this->getUsers() as $userObject) {
$user = User::all()->random()->id;
$profile = DB::table('user_profiles')->insertGetId([
'user_id' => $user,
'status' => $faker->numberBetween($min = 1, $max = 2),
'first_name' => $faker->firstName(),
'last_name' => $faker->lastName(),
'job' => $faker->company,
'distance' => $faker->numberBetween($min = 1, $max = 1000),
'city' => $faker->city,
'interested_in' => $faker->numberBetween($min = 1, $max = 2),
'age_from_preference' => $faker->numberBetween($min = 18, $max = 35),
'age_to_preference' => $faker->numberBetween($min = 36, $max = 66),
'looking_for' => $faker->numberBetween($min = 1, $max = 5),
'personal_quote' => $faker->text(),
'bio_about' => $faker->text(),
'postal_code' => Address::postcode(),
'country' => $faker->country,
'height' => $faker->numberBetween($min = 140, $max = 220),
'orientation' => $faker->numberBetween($min = 1, $max = 3),
'sexual_preferences' => $faker->numberBetween($min = 1, $max = 4),
'body_type' => $faker->numberBetween($min = 1, $max = 4),
'relationship_status' => $faker->numberBetween($min = 1, $max = 5),
'children' => $faker->numberBetween($min = 1, $max = 5),
'smoke' => $faker->numberBetween($min = 1, $max = 3),
'education' => $faker->numberBetween($min = 1, $max = 4),
'astro' => $faker->numberBetween($min = 1, $max = 12),
'hobbies' => $faker->sentence(),
'workplace' => $faker->jobTitle,
'sports' => $faker->numberBetween($min = 1, $max = 12),
'movies_series' => $faker->numberBetween($min = 1, $max = 12),
'music' => $faker->numberBetween($min = 1, $max = 12),
'food' => $faker->numberBetween($min = 1, $max = 12),
'literature' => $faker->numberBetween($min = 1, $max = 12),
'mobile_number' => $faker->phoneNumber,
'updated_at' => Carbon::now(),
]);
}
}
public function getUsers()
{
$jsonData = '[
{
"name":"authuser"
},
{
"name":"normaluser"
},
{
"name":"normaluser2"
},
{
"name":"normaluser3" // here is that user that is supposed to be in for loop!!!!!!!!!!!!!
}
]';
return json_decode($jsonData);
}
}
将 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
]);
}
我有用户播种器和用户个人资料播种器。目前,当我为我的数据库播种时,我在用户 table 中得到了一百五十个用户,只有四个用户的个人资料填写在 user_profiles table 中。现在,我知道问题出在 UserProfileSeeder.php 中的 getUsers() 函数中,returns json_decode 结果只有四个用户。我需要有关如何为第四个用户 (normaluser3) 编写 'for' 循环的帮助,以便我在 user_profiles table 中获得正确数量的结果 (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(),
]
]);
}
}
}
UserProfileSeeder.php
<?php
use App\User;
use Carbon\Carbon;
use Faker\Provider\Address;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
class UserProfileSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$faker = Faker\Factory::create();
foreach ($this->getUsers() as $userObject) {
$user = User::all()->random()->id;
$profile = DB::table('user_profiles')->insertGetId([
'user_id' => $user,
'status' => $faker->numberBetween($min = 1, $max = 2),
'first_name' => $faker->firstName(),
'last_name' => $faker->lastName(),
'job' => $faker->company,
'distance' => $faker->numberBetween($min = 1, $max = 1000),
'city' => $faker->city,
'interested_in' => $faker->numberBetween($min = 1, $max = 2),
'age_from_preference' => $faker->numberBetween($min = 18, $max = 35),
'age_to_preference' => $faker->numberBetween($min = 36, $max = 66),
'looking_for' => $faker->numberBetween($min = 1, $max = 5),
'personal_quote' => $faker->text(),
'bio_about' => $faker->text(),
'postal_code' => Address::postcode(),
'country' => $faker->country,
'height' => $faker->numberBetween($min = 140, $max = 220),
'orientation' => $faker->numberBetween($min = 1, $max = 3),
'sexual_preferences' => $faker->numberBetween($min = 1, $max = 4),
'body_type' => $faker->numberBetween($min = 1, $max = 4),
'relationship_status' => $faker->numberBetween($min = 1, $max = 5),
'children' => $faker->numberBetween($min = 1, $max = 5),
'smoke' => $faker->numberBetween($min = 1, $max = 3),
'education' => $faker->numberBetween($min = 1, $max = 4),
'astro' => $faker->numberBetween($min = 1, $max = 12),
'hobbies' => $faker->sentence(),
'workplace' => $faker->jobTitle,
'sports' => $faker->numberBetween($min = 1, $max = 12),
'movies_series' => $faker->numberBetween($min = 1, $max = 12),
'music' => $faker->numberBetween($min = 1, $max = 12),
'food' => $faker->numberBetween($min = 1, $max = 12),
'literature' => $faker->numberBetween($min = 1, $max = 12),
'mobile_number' => $faker->phoneNumber,
'updated_at' => Carbon::now(),
]);
}
}
public function getUsers()
{
$jsonData = '[
{
"name":"authuser"
},
{
"name":"normaluser"
},
{
"name":"normaluser2"
},
{
"name":"normaluser3" // here is that user that is supposed to be in for loop!!!!!!!!!!!!!
}
]';
return json_decode($jsonData);
}
}
将 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
]);
}