forked from summerblue/larabbs
-
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
1 parent
3eaa3ee
commit e9fb2c6
Showing
5 changed files
with
94 additions
and
25 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
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, | ||
]; | ||
}); |
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,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(); | ||
|
||
} | ||
} |