forked from summerblue/larabbs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUsersTableSeeder.php
55 lines (45 loc) · 1.84 KB
/
UsersTableSeeder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?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();
// 初始化用户角色,将 1 号用户指派为『站长』
$user->assignRole('Founder');
// 将 2 号用户指派为『管理员』
$user = User::find(2);
$user->assignRole('Maintainer');
}
}