Teacher.php 978 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. return $this->belongsTo(User::class, 'id', 'user_id');
  27. }
  28. /**
  29. * 老师下的学生。
  30. */
  31. public function students(): HasMany
  32. {
  33. return $this->hasMany(Student::class, 'teacher_id', 'teacher_id');
  34. }
  35. }