study-api-v2/database/seeders/StudentSeeder.php

112 lines
4.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace Database\Seeders;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use App\Models\Students\Student;
class StudentSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
// 创建一些测试学生数据
$students = [
[
'username' => 'student001',
'real_name' => '张三',
'password' => Student::encryptPassword('123456', 'abc123'),
'salt' => 'abc123',
'role' => Student::ROLE_STUDENT,
'grade_id' => 1,
'email' => 'student001@example.com',
'phone_number' => '13800138001',
'status' => Student::STATUS_NORMAL,
'sex' => Student::SEX_MALE,
'reg_time' => now(),
'reg_ip' => '127.0.0.1',
],
[
'username' => 'student002',
'real_name' => '李四',
'password' => Student::encryptPassword('123456', 'def456'),
'salt' => 'def456',
'role' => Student::ROLE_STUDENT,
'grade_id' => 1,
'email' => 'student002@example.com',
'phone_number' => '13800138002',
'status' => Student::STATUS_NORMAL,
'sex' => Student::SEX_FEMALE,
'reg_time' => now(),
'reg_ip' => '127.0.0.1',
],
[
'username' => 'student003',
'real_name' => '王五',
'password' => Student::encryptPassword('123456', 'ghi789'),
'salt' => 'ghi789',
'role' => Student::ROLE_STUDENT,
'grade_id' => 2,
'email' => 'student003@example.com',
'phone_number' => '13800138003',
'status' => Student::STATUS_NORMAL,
'sex' => Student::SEX_MALE,
'reg_time' => now(),
'reg_ip' => '127.0.0.1',
],
[
'username' => 'parent001',
'real_name' => '张父',
'password' => Student::encryptPassword('123456', 'jkl012'),
'salt' => 'jkl012',
'role' => Student::ROLE_PARENT,
'email' => 'parent001@example.com',
'phone_number' => '13800138101',
'status' => Student::STATUS_NORMAL,
'sex' => Student::SEX_MALE,
'reg_time' => now(),
'reg_ip' => '127.0.0.1',
],
[
'username' => 'parent002',
'real_name' => '李母',
'password' => Student::encryptPassword('123456', 'mno345'),
'salt' => 'mno345',
'role' => Student::ROLE_PARENT,
'email' => 'parent002@example.com',
'phone_number' => '13800138102',
'status' => Student::STATUS_NORMAL,
'sex' => Student::SEX_FEMALE,
'reg_time' => now(),
'reg_ip' => '127.0.0.1',
],
];
foreach ($students as $studentData) {
Student::create($studentData);
}
// 设置家长和子女关系
$parent1 = Student::where('username', 'parent001')->first();
$parent2 = Student::where('username', 'parent002')->first();
$student1 = Student::where('username', 'student001')->first();
$student2 = Student::where('username', 'student002')->first();
if ($parent1 && $student1) {
$student1->update(['parent_id' => $parent1->id]);
}
if ($parent2 && $student2) {
$student2->update(['parent_id' => $parent2->id]);
}
$this->command->info('学生测试数据创建完成!');
$this->command->info('创建了3个学生和2个家长密码都是123456');
$this->command->info('学生用户名student001, student002, student003');
$this->command->info('家长用户名parent001, parent002');
}
}