Skip to content

Commit

Permalink
填充用户和话题数据
Browse files Browse the repository at this point in the history
  • Loading branch information
summerblue committed Sep 19, 2019
1 parent 3eaa3ee commit e9fb2c6
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 25 deletions.
15 changes: 14 additions & 1 deletion database/factories/TopicFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,20 @@
use Faker\Generator as Faker;

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

$sentence = $faker->sentence();

// 随机取一个月以内的时间
$updated_at = $faker->dateTimeThisMonth();

// 传参为生成最大时间不超过,因为创建时间需永远比更改时间要早
$created_at = $faker->dateTimeThisMonth($updated_at);

return [
// 'name' => $faker->name,
'title' => $sentence,
'body' => $faker->text(),
'excerpt' => $sentence,
'created_at' => $created_at,
'updated_at' => $updated_at,
];
});
25 changes: 8 additions & 17 deletions database/factories/UserFactory.php
Original file line number Diff line number Diff line change
@@ -1,27 +1,18 @@
<?php

/** @var \Illuminate\Database\Eloquent\Factory $factory */
use App\Models\User;
use Faker\Generator as Faker;
use Illuminate\Support\Str;
use Faker\Generator as Faker;

/*
|--------------------------------------------------------------------------
| Model Factories
|--------------------------------------------------------------------------
|
| This directory should contain each of the model factory definitions for
| your application. Factories provide a convenient way to generate new
| model instances for testing / seeding your application's database.
|
*/

$factory->define(User::class, function (Faker $faker) {
$factory->define(App\Models\User::class, function (Faker $faker) {
$date_time = $faker->date . ' ' . $faker->time;
return [
'name' => $faker->name,
'email' => $faker->unique()->safeEmail,
'email_verified_at' => now(),
'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
'remember_token' => Str::random(10),
'email_verified_at' => now(),
'password' => '$2y$10$TKh8H1.PfQx37YgCzwiKb.KjNyWgaHb9cbcoQgdIVFlYg7B77UdFm', // secret
'introduction' => $faker->sentence(),
'created_at' => $date_time,
'updated_at' => $date_time,
];
});
2 changes: 1 addition & 1 deletion database/seeds/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class DatabaseSeeder extends Seeder
*/
public function run()
{
// $this->call(UsersTableSeeder::class);
$this->call(UsersTableSeeder::class);
$this->call(TopicsTableSeeder::class);
}
}
29 changes: 23 additions & 6 deletions database/seeds/TopicsTableSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,36 @@

use Illuminate\Database\Seeder;
use App\Models\Topic;
use App\Models\User;
use App\Models\Category;

class TopicsTableSeeder extends Seeder
{
public function run()
{
$topics = factory(Topic::class)->times(50)->make()->each(function ($topic, $index) {
if ($index == 0) {
// $topic->field = 'value';
}
// 所有用户 ID 数组,如:[1,2,3,4]
$user_ids = User::all()->pluck('id')->toArray();

// 所有分类 ID 数组,如:[1,2,3,4]
$category_ids = Category::all()->pluck('id')->toArray();

// 获取 Faker 实例
$faker = app(Faker\Generator::class);

$topics = factory(Topic::class)
->times(100)
->make()
->each(function ($topic, $index)
use ($user_ids, $category_ids, $faker)
{
// 从用户 ID 数组中随机取出一个并赋值
$topic->user_id = $faker->randomElement($user_ids);

// 话题分类,同上
$topic->category_id = $faker->randomElement($category_ids);
});

// 将数据集合转换为数组,并插入到数据库中
Topic::insert($topics->toArray());
}

}

48 changes: 48 additions & 0 deletions database/seeds/UsersTableSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

use Illuminate\Database\Seeder;
use App\Models\User;

class UsersTableSeeder extends Seeder
{
public function run()
{
// 获取 Faker 实例
$faker = app(Faker\Generator::class);

// 头像假数据
$avatars = [
'https://cdn.learnku.com/uploads/images/201710/14/1/s5ehp11z6s.png',
'https://cdn.learnku.com/uploads/images/201710/14/1/Lhd1SHqu86.png',
'https://cdn.learnku.com/uploads/images/201710/14/1/LOnMrqbHJn.png',
'https://cdn.learnku.com/uploads/images/201710/14/1/xAuDMxteQy.png',
'https://cdn.learnku.com/uploads/images/201710/14/1/ZqM7iaP4CR.png',
'https://cdn.learnku.com/uploads/images/201710/14/1/NDnzMutoxX.png',
];

// 生成数据集合
$users = factory(User::class)
->times(10)
->make()
->each(function ($user, $index)
use ($faker, $avatars)
{
// 从头像数组中随机取出一个并赋值
$user->avatar = $faker->randomElement($avatars);
});

// 让隐藏字段可见,并将数据集合转换为数组
$user_array = $users->makeVisible(['password', 'remember_token'])->toArray();

// 插入到数据库中
User::insert($user_array);

// 单独处理第一个用户的数据
$user = User::find(1);
$user->name = 'Summer';
$user->email = '[email protected]';
$user->avatar = 'https://cdn.learnku.com/uploads/images/201710/14/1/ZqM7iaP4CR.png';
$user->save();

}
}

0 comments on commit e9fb2c6

Please sign in to comment.