| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- <?php
- namespace App\Models;
- use Illuminate\Database\Eloquent\Factories\HasFactory;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Database\Eloquent\Relations\BelongsTo;
- use Illuminate\Database\Eloquent\Relations\HasMany;
- class Teacher extends Model
- {
- use HasFactory;
- protected $table = 'teachers';
- protected $primaryKey = 'teacher_id';
- public $incrementing = false;
- protected $keyType = 'string';
- public $timestamps = true;
- protected $fillable = [
- 'teacher_id',
- 'teacher_string_id',
- 'user_id',
- 'name',
- 'subject',
- ];
- /**
- * 对应老师用户信息。
- */
- public function user(): BelongsTo
- {
- // teachers.user_id 对应 users.user_id(而非默认的 id)
- return $this->belongsTo(User::class, 'user_id', 'user_id');
- }
- /**
- * 老师下的学生。
- */
- public function students(): HasMany
- {
- return $this->hasMany(Student::class, 'teacher_id', 'teacher_id');
- }
- }
|