48 lines
1.1 KiB
PHP
48 lines
1.1 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace App\Services;
|
||
|
|
||
|
use App\Models\Subject;
|
||
|
use App\Models\Teacher;
|
||
|
use Illuminate\Database\Eloquent\Collection;
|
||
|
use Illuminate\Database\Eloquent\Model;
|
||
|
|
||
|
class GradeTeacherService implements ServiceInterface
|
||
|
{
|
||
|
public function getAll(?Teacher $teacher = null, ?Subject $subject = null): Collection
|
||
|
{
|
||
|
return $teacher
|
||
|
->grades()
|
||
|
->join('grade_subject', 'grades.id', '=', 'grade_subject.grade_id')
|
||
|
->where('subject_id', $subject->id)
|
||
|
->get();
|
||
|
}
|
||
|
|
||
|
public function getGrades(Subject $subject): Collection
|
||
|
{
|
||
|
return $subject->grades;
|
||
|
}
|
||
|
|
||
|
public function create(array $data, ?Model $model = null): Teacher
|
||
|
{
|
||
|
$model->grades()->syncWithoutDetaching($data['grade_id']);
|
||
|
|
||
|
return $model;
|
||
|
}
|
||
|
|
||
|
public function update(Model $model, array $data, ?Model $grade = null): Teacher
|
||
|
{
|
||
|
$model->grades()->detach($grade->id);
|
||
|
$model->grades()->attach($data['grade_id']);
|
||
|
|
||
|
return $model;
|
||
|
}
|
||
|
|
||
|
public function delete(Model $model, ?Model $grade = null): Teacher
|
||
|
{
|
||
|
$model->grades()->detach($grade);
|
||
|
|
||
|
return $model;
|
||
|
}
|
||
|
}
|