refactor: update Eloquent models to use guarded attributes and consolidate relationship definitions
This commit is contained in:
parent
c460fec1f2
commit
0b8d4ef09b
@ -16,14 +16,7 @@ class Assignment extends Model implements HasMedia
|
||||
{
|
||||
use HasFactory, InteractsWithMedia, SoftDeletes;
|
||||
|
||||
protected $fillable = [
|
||||
'course_id',
|
||||
'class_session_id',
|
||||
'description',
|
||||
'due_date',
|
||||
'title',
|
||||
'type',
|
||||
];
|
||||
protected $guarded = ['id'];
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
@ -48,16 +41,16 @@ public function assignmentTargets(): HasMany
|
||||
return $this->hasMany(AssignmentTarget::class);
|
||||
}
|
||||
|
||||
public function course(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Course::class);
|
||||
}
|
||||
|
||||
public function classSession(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(ClassSession::class);
|
||||
}
|
||||
|
||||
public function course(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Course::class);
|
||||
}
|
||||
|
||||
public function students(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(
|
||||
|
||||
@ -7,10 +7,7 @@
|
||||
|
||||
class AssignmentPin extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'assignment_id',
|
||||
'student_id',
|
||||
];
|
||||
protected $guarded = ['id'];
|
||||
|
||||
public function assignment(): BelongsTo
|
||||
{
|
||||
|
||||
@ -12,13 +12,7 @@ class AssignmentSubmission extends Model implements HasMedia
|
||||
{
|
||||
use HasFactory, InteractsWithMedia;
|
||||
|
||||
protected $fillable = [
|
||||
'assignment_id',
|
||||
'notes',
|
||||
'student_id',
|
||||
'study_group_id',
|
||||
'submitted_at',
|
||||
];
|
||||
protected $guarded = ['id'];
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
|
||||
@ -10,11 +10,7 @@ class AssignmentTarget extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $fillable = [
|
||||
'assignment_id',
|
||||
'student_id',
|
||||
'study_group_id',
|
||||
];
|
||||
protected $guarded = ['id'];
|
||||
|
||||
public function assignment(): BelongsTo
|
||||
{
|
||||
|
||||
@ -3,6 +3,7 @@
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class Attendance extends Model
|
||||
{
|
||||
@ -16,18 +17,18 @@ protected function casts(): array
|
||||
];
|
||||
}
|
||||
|
||||
public function student()
|
||||
public function classSession(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Student::class);
|
||||
return $this->belongsTo(ClassSession::class);
|
||||
}
|
||||
|
||||
public function courseSchedule()
|
||||
public function courseSchedule(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(CourseSchedule::class);
|
||||
}
|
||||
|
||||
public function classSession()
|
||||
public function student(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(ClassSession::class);
|
||||
return $this->belongsTo(Student::class);
|
||||
}
|
||||
}
|
||||
|
||||
@ -12,29 +12,16 @@ class ClassSession extends Model
|
||||
{
|
||||
use HasFactory, SoftDeletes;
|
||||
|
||||
protected $fillable = [
|
||||
'course_id',
|
||||
'session_number',
|
||||
'date',
|
||||
'start_time',
|
||||
'end_time',
|
||||
];
|
||||
protected $guarded = ['id'];
|
||||
|
||||
protected $casts = [
|
||||
'date' => 'date',
|
||||
'session_number' => 'integer',
|
||||
'start_time' => 'datetime',
|
||||
'end_time' => 'datetime',
|
||||
];
|
||||
|
||||
public function course(): BelongsTo
|
||||
protected function casts(): array
|
||||
{
|
||||
return $this->belongsTo(Course::class);
|
||||
}
|
||||
|
||||
public function materials(): HasMany
|
||||
{
|
||||
return $this->hasMany(Material::class);
|
||||
return [
|
||||
'date' => 'date',
|
||||
'session_number' => 'integer',
|
||||
'start_time' => 'datetime',
|
||||
'end_time' => 'datetime',
|
||||
];
|
||||
}
|
||||
|
||||
public function assignments(): HasMany
|
||||
@ -46,4 +33,14 @@ public function attendances(): HasMany
|
||||
{
|
||||
return $this->hasMany(Attendance::class);
|
||||
}
|
||||
|
||||
public function course(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Course::class);
|
||||
}
|
||||
|
||||
public function materials(): HasMany
|
||||
{
|
||||
return $this->hasMany(Material::class);
|
||||
}
|
||||
}
|
||||
|
||||
@ -21,6 +21,21 @@ protected static function booted()
|
||||
});
|
||||
}
|
||||
|
||||
public function assignments(): HasMany
|
||||
{
|
||||
return $this->hasMany(Assignment::class);
|
||||
}
|
||||
|
||||
public function classSessions(): HasMany
|
||||
{
|
||||
return $this->hasMany(ClassSession::class);
|
||||
}
|
||||
|
||||
public function courseSchedules(): HasMany
|
||||
{
|
||||
return $this->hasMany(CourseSchedule::class);
|
||||
}
|
||||
|
||||
public function materials(): HasMany
|
||||
{
|
||||
return $this->hasMany(Material::class);
|
||||
@ -30,14 +45,4 @@ public function studyGroups(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(StudyGroup::class, 'study_group_courses');
|
||||
}
|
||||
|
||||
public function courseSchedules(): HasMany
|
||||
{
|
||||
return $this->hasMany(CourseSchedule::class);
|
||||
}
|
||||
|
||||
public function classSessions(): HasMany
|
||||
{
|
||||
return $this->hasMany(ClassSession::class);
|
||||
}
|
||||
}
|
||||
|
||||
@ -8,6 +8,7 @@
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
|
||||
@ -37,13 +38,53 @@ protected function female(Builder $query): void
|
||||
$query->where('sex', Sex::Female);
|
||||
}
|
||||
|
||||
public function user(): BelongsTo
|
||||
public function assignmentPins(): HasMany
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
return $this->hasMany(AssignmentPin::class);
|
||||
}
|
||||
|
||||
public function assignments(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(
|
||||
Assignment::class,
|
||||
'assignment_targets',
|
||||
'student_id',
|
||||
'assignment_id'
|
||||
);
|
||||
}
|
||||
|
||||
public function assignmentSubmissions(): HasMany
|
||||
{
|
||||
return $this->hasMany(AssignmentSubmission::class);
|
||||
}
|
||||
|
||||
public function assignmentTargets(): HasMany
|
||||
{
|
||||
return $this->hasMany(AssignmentTarget::class);
|
||||
}
|
||||
|
||||
public function attendances(): HasMany
|
||||
{
|
||||
return $this->hasMany(Attendance::class);
|
||||
}
|
||||
|
||||
public function ledStudyGroups(): HasMany
|
||||
{
|
||||
return $this->hasMany(StudyGroup::class, 'leader_id');
|
||||
}
|
||||
|
||||
public function studyGroupMembers(): HasMany
|
||||
{
|
||||
return $this->hasMany(StudyGroupMember::class);
|
||||
}
|
||||
|
||||
public function studyGroups(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(StudyGroup::class, 'study_group_members');
|
||||
}
|
||||
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
}
|
||||
|
||||
@ -13,16 +13,33 @@ class StudyGroup extends Model
|
||||
{
|
||||
use HasFactory, SoftDeletes;
|
||||
|
||||
protected $fillable = [
|
||||
'leader_id',
|
||||
'name',
|
||||
];
|
||||
protected $guarded = ['id'];
|
||||
|
||||
public function isLeader(Student $student): bool
|
||||
{
|
||||
return $this->leader_id === $student->id;
|
||||
}
|
||||
|
||||
public function assignmentSubmissions(): HasMany
|
||||
{
|
||||
return $this->hasMany(AssignmentSubmission::class);
|
||||
}
|
||||
|
||||
public function assignmentTargets(): HasMany
|
||||
{
|
||||
return $this->hasMany(AssignmentTarget::class);
|
||||
}
|
||||
|
||||
public function assignments(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(
|
||||
Assignment::class,
|
||||
'assignment_targets',
|
||||
'study_group_id',
|
||||
'assignment_id'
|
||||
);
|
||||
}
|
||||
|
||||
public function courses(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(Course::class, 'study_group_courses');
|
||||
|
||||
@ -10,10 +10,7 @@ class StudyGroupMember extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $fillable = [
|
||||
'student_id',
|
||||
'study_group_id',
|
||||
];
|
||||
protected $guarded = ['id'];
|
||||
|
||||
public function student(): BelongsTo
|
||||
{
|
||||
|
||||
Loading…
Reference in New Issue
Block a user