6
0
Fork 0
jiqu-library-server/database/seeders/UserSeeder.php

90 lines
2.2 KiB
PHP

<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
use App\Models\User;
use Illuminate\Support\Facades\DB;
use Faker\Generator;
use Illuminate\Container\Container;
class UserSeeder extends Seeder
{
protected $faker;
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
DB::table('user_infos')->truncate();
DB::table('user_vips')->truncate();
DB::table('socialite_users')->truncate();
DB::table('personal_access_tokens')->truncate();
DB::table('wallets')->truncate();
DB::table('balances')->truncate();
DB::table('users')->truncate();
$this->faker = $this->withFaker();
$this->createUsers(1);
}
protected function createUsers($vip, $parent = null)
{
if ($vip > 7) {
return;
}
$faker = $this->faker;
for ($i = 0; $i < $faker->numberBetween(1, 5); $i++) {
$user = $this->createUser($vip, $parent);
$this->createUsers($vip + 1, $user);
}
}
protected function createUser($vip = null, $inviter = null)
{
$faker = $this->faker;
$time = now();
$phone = $this->fakePhone();
$email = $this->fakeEmail();
$user = User::create([
'phone' => $phone,
'phone_verified_at' => $time,
'email' => $email,
'email_verified_at' => $time,
'register_ip' => $faker->ipv4,
'status' => 1
], $inviter);
if ($vip && $vip <= 6) {
$user->userVip()->create([
'vip_id' => $vip
]);
}
return $user;
}
protected function fakePhone()
{
do {
$phone = substr($this->faker->e164PhoneNumber, 1);
} while(User::where('phone', $phone)->exists());
return $phone;
}
protected function fakeEmail()
{
do {
$email = $this->faker->email;
} while(User::where('email', $email)->exists());
return $email;
}
protected function withFaker()
{
return Container::getInstance()->make(Generator::class);
}
}