Student.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace App\Models;
  3. use App\Services\StudentIdService;
  4. use Illuminate\Database\Eloquent\Factories\HasFactory;
  5. use Illuminate\Database\Eloquent\Model;
  6. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  7. class Student extends Model
  8. {
  9. use HasFactory;
  10. protected $table = 'students';
  11. protected $primaryKey = 'student_id';
  12. public $incrementing = false;
  13. protected $keyType = 'int';
  14. protected $fillable = [
  15. 'student_id',
  16. 'name',
  17. 'grade',
  18. 'class_name',
  19. 'teacher_id',
  20. 'remark',
  21. ];
  22. protected $casts = [
  23. 'created_at' => 'datetime',
  24. 'updated_at' => 'datetime',
  25. ];
  26. protected static function boot()
  27. {
  28. parent::boot();
  29. // 在创建学生时自动生成ID并创建用户记录
  30. static::creating(function (Student $student) {
  31. if (empty($student->student_id)) {
  32. $student->student_id = StudentIdService::generateStudentId();
  33. }
  34. // 创建对应的用户记录(需要在学生记录之前创建,因为存在外键约束)
  35. $existingUser = \App\Models\User::where('user_id', $student->student_id)->first();
  36. if (!$existingUser) {
  37. \App\Models\User::create([
  38. 'user_id' => $student->student_id,
  39. 'username' => $student->student_id,
  40. 'password_hash' => \Hash::make('password123'), // 默认密码,实际使用时应该修改
  41. 'role' => 'student',
  42. 'full_name' => $student->name,
  43. 'email' => $student->student_id . '@student.edu',
  44. 'is_active' => 1,
  45. ]);
  46. }
  47. });
  48. // 在学生数据变更后清除缓存
  49. static::saved(function (Student $student) {
  50. \App\Filament\Resources\StudentResource::clearCaches();
  51. });
  52. static::deleted(function (Student $student) {
  53. \App\Filament\Resources\StudentResource::clearCaches();
  54. });
  55. }
  56. public function teacher(): BelongsTo
  57. {
  58. return $this->belongsTo(Teacher::class, 'teacher_id', 'teacher_id');
  59. }
  60. public function user(): BelongsTo
  61. {
  62. return $this->belongsTo(User::class, 'student_id', 'user_id');
  63. }
  64. /**
  65. * 获取学生的试卷列表
  66. */
  67. public function papers()
  68. {
  69. return $this->hasMany(\App\Models\Paper::class, 'student_id', 'student_id');
  70. }
  71. /**
  72. * 获取学生最近生成的试卷
  73. */
  74. public function recentPapers($limit = 10)
  75. {
  76. return $this->hasMany(\App\Models\Paper::class, 'student_id', 'student_id')
  77. ->orderBy('created_at', 'desc')
  78. ->limit($limit);
  79. }
  80. /**
  81. * 获取学生的学情报告
  82. */
  83. public function reports()
  84. {
  85. return $this->hasMany(\App\Models\StudentReport::class, 'student_id', 'student_id');
  86. }
  87. /**
  88. * 获取学生最新的学情报告
  89. */
  90. public function latestReport()
  91. {
  92. return $this->hasOne(\App\Models\StudentReport::class, 'student_id', 'student_id')
  93. ->latest('generated_at');
  94. }
  95. }