Teacher.php 1023 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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 = 'string';
  14. public $timestamps = true;
  15. protected $fillable = [
  16. 'teacher_id',
  17. 'teacher_string_id',
  18. 'user_id',
  19. 'name',
  20. 'subject',
  21. ];
  22. /**
  23. * 对应老师用户信息。
  24. */
  25. public function user(): BelongsTo
  26. {
  27. // teachers.user_id 对应 users.user_id(而非默认的 id)
  28. return $this->belongsTo(User::class, 'user_id', 'user_id');
  29. }
  30. /**
  31. * 老师下的学生。
  32. */
  33. public function students(): HasMany
  34. {
  35. return $this->hasMany(Student::class, 'teacher_id', 'teacher_id');
  36. }
  37. }