-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
vagrant
committed
Apr 4, 2018
1 parent
253f196
commit fe16cd6
Showing
5 changed files
with
102 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 (); | ||
|
||
} | ||
} |