Teacher.php 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Factories\HasFactory;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  6. use Illuminate\Database\Eloquent\Relations\HasMany;
  7. class Teacher extends Model
  8. {
  9. use HasFactory;
  10. protected $table = 'teachers';
  11. protected $primaryKey = 'teacher_id';
  12. public $incrementing = false;
  13. protected $keyType = 'int';
  14. public $timestamps = false; // 禁用时间戳,因为数据库表没有 updated_at 字段
  15. protected $fillable = [
  16. 'teacher_id',
  17. 'user_id',
  18. 'name',
  19. 'subject',
  20. ];
  21. /**
  22. * 对应老师用户信息。
  23. */
  24. public function user(): BelongsTo
  25. {
  26. // teachers.user_id 对应 users.user_id(而非默认的 id)
  27. return $this->belongsTo(User::class, 'user_id', 'user_id');
  28. }
  29. /**
  30. * 老师下的学生。
  31. */
  32. public function students(): HasMany
  33. {
  34. return $this->hasMany(Student::class, 'teacher_id', 'teacher_id');
  35. }
  36. }