2024-05-06 17:47:25 +04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
|
|
use App\Models\Subject;
|
|
|
|
use App\Models\Teacher;
|
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
|
|
|
|
class SubjectTeacherService implements ServiceInterface
|
|
|
|
{
|
2024-05-06 17:51:16 +04:00
|
|
|
public function getAll(?Teacher $teacher = null): Collection
|
2024-05-06 17:47:25 +04:00
|
|
|
{
|
|
|
|
return $teacher->subjects;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getAllSubjects(): Collection
|
|
|
|
{
|
|
|
|
return Subject::all();
|
|
|
|
}
|
|
|
|
|
2024-05-07 15:05:10 +04:00
|
|
|
public function getGrades(Teacher $teacher, Subject $subject): Collection
|
|
|
|
{
|
|
|
|
return $teacher
|
|
|
|
->grades()
|
|
|
|
->join('grade_subject', 'grades.id', '=', 'grade_subject.grade_id')
|
|
|
|
->where('subject_id', $subject->id)->get();
|
|
|
|
}
|
|
|
|
|
2024-05-06 17:51:16 +04:00
|
|
|
public function create(array $data, ?Model $model = null): Teacher
|
2024-05-06 17:47:25 +04:00
|
|
|
{
|
|
|
|
$model->subjects()->syncWithoutDetaching($data['subject_id']);
|
|
|
|
|
|
|
|
return $model;
|
|
|
|
}
|
|
|
|
|
2024-05-06 17:51:16 +04:00
|
|
|
public function update(Model $model, array $data, ?Model $subject = null): Teacher
|
2024-05-06 17:47:25 +04:00
|
|
|
{
|
|
|
|
$model->subjects()->detach($subject->id);
|
|
|
|
$model->subjects()->attach($data['subject_id']);
|
|
|
|
|
|
|
|
return $model;
|
|
|
|
}
|
|
|
|
|
2024-05-06 17:51:16 +04:00
|
|
|
public function delete(Model $model, ?Model $subject = null): Teacher
|
2024-05-06 17:47:25 +04:00
|
|
|
{
|
|
|
|
$model->subjects()->detach($subject);
|
|
|
|
|
|
|
|
return $model;
|
|
|
|
}
|
|
|
|
}
|