Skip to content

Commit

Permalink
Seeding topics and users data
Browse files Browse the repository at this point in the history
  • Loading branch information
vagrant committed Apr 4, 2018
1 parent 253f196 commit fe16cd6
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 12 deletions.
14 changes: 13 additions & 1 deletion database/factories/TopicFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,19 @@
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,
];
});
9 changes: 8 additions & 1 deletion database/factories/UserFactory.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Carbon\Carbon;
use Faker\Generator as Faker;

/*
Expand All @@ -14,10 +15,16 @@
*/

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

return [
'name' => $faker->name,
'email' => $faker->unique()->safeEmail,
'password' => '$2y$10$TKh8H1.PfQx37YgCzwiKb.KjNyWgaHb9cbcoQgdIVFlYg7B77UdFm', // secret
'password' => $password ?: $password = bcrypt('password'),
'remember_token' => str_random(10),
'introduction' => $faker->sentence(),
'created_at' => $now,
'updated_at' => $now,
];
});
6 changes: 3 additions & 3 deletions database/seeds/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ class DatabaseSeeder extends Seeder
*
* @return void
*/
public function run()
public function run ()
{
// $this->call(UsersTableSeeder::class);
$this->call(TopicsTableSeeder::class);
$this->call ( UsersTableSeeder::class );
$this->call ( TopicsTableSeeder::class );
}
}
33 changes: 26 additions & 7 deletions database/seeds/TopicsTableSeeder.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,38 @@
<?php

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

class TopicsTableSeeder extends Seeder
{
public function run()
public function run ()
{
$topics = factory(Topic::class)->times(50)->make()->each(function ($topic, $index) {
if ($index == 0) {
// $topic->field = 'value';
}
});

Topic::insert($topics->toArray());
// 所有用户 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 () );

}

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

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

class UsersTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run ()
{
// 获取 Faker 实例
$faker = app ( Faker\Generator::class );

// 头像假数据
$avatars = [
'https://fsdhubcdn.phphub.org/uploads/images/201710/14/1/s5ehp11z6s.png?imageView2/1/w/200/h/200' ,
'https://fsdhubcdn.phphub.org/uploads/images/201710/14/1/Lhd1SHqu86.png?imageView2/1/w/200/h/200' ,
'https://fsdhubcdn.phphub.org/uploads/images/201710/14/1/LOnMrqbHJn.png?imageView2/1/w/200/h/200' ,
'https://fsdhubcdn.phphub.org/uploads/images/201710/14/1/xAuDMxteQy.png?imageView2/1/w/200/h/200' ,
'https://fsdhubcdn.phphub.org/uploads/images/201710/14/1/ZqM7iaP4CR.png?imageView2/1/w/200/h/200' ,
'https://fsdhubcdn.phphub.org/uploads/images/201710/14/1/NDnzMutoxX.png?imageView2/1/w/200/h/200' ,
];

// 生成数据集合
$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 = 'cuijianguo';
$user->email = '[email protected]';
$user->avatar = 'https://fsdhubcdn.phphub.org/uploads/images/201710/14/1/ZqM7iaP4CR.png?imageView2/1/w/200/h/200';
$user->save ();

}
}

0 comments on commit fe16cd6

Please sign in to comment.