CourseWork/app/Services/StudentService.php

57 lines
1.4 KiB
PHP
Raw Permalink Normal View History

2024-04-23 14:52:49 +04:00
<?php
namespace App\Services;
2024-05-27 18:40:22 +04:00
use App\Enums\ScoreEnum;
2024-04-23 14:52:49 +04:00
use App\Models\Student;
use App\Models\User;
use Illuminate\Pagination\LengthAwarePaginator;
2024-06-16 12:20:48 +04:00
class StudentService
2024-04-23 14:52:49 +04:00
{
2024-04-27 15:10:21 +04:00
public function create(array $data): Student
2024-04-23 14:52:49 +04:00
{
2024-04-27 15:10:21 +04:00
$user = User::create([
'email' => $data['email'],
'password' => $data['password'],
]);
$student = Student::create([
'name' => $data['name'],
'last_name' => $data['last_name'],
'middle_name' => $data['middle_name'],
'birthday' => $data['birthday'],
'grade_id' => $data['grade_id'],
]);
2024-04-23 14:52:49 +04:00
$student->user()->save($user);
2024-05-27 18:40:22 +04:00
$student
->lessons()
->syncWithPivotValues($student->grade->lessons->pluck('id')->all(), ['score' => ScoreEnum::WithoutScore]);
2024-04-23 14:52:49 +04:00
return $student;
}
2024-06-16 12:20:48 +04:00
public function update(Student $student, array $data): Student
2024-04-23 14:52:49 +04:00
{
2024-06-16 12:20:48 +04:00
$student->user()->update([
2024-04-27 15:10:21 +04:00
'email' => $data['email'],
'password' => $data['password'],
]);
2024-06-16 12:20:48 +04:00
$student->update([
2024-04-27 15:10:21 +04:00
'name' => $data['name'],
'last_name' => $data['last_name'],
'middle_name' => $data['middle_name'],
'birthday' => $data['birthday'],
'grade_id' => $data['grade_id'],
]);
2024-04-23 14:52:49 +04:00
2024-06-16 12:20:48 +04:00
return $student;
2024-04-23 14:52:49 +04:00
}
public function delete($model): void
{
2024-04-27 15:10:21 +04:00
$model->user()->delete();
$model->delete();
2024-04-23 14:52:49 +04:00
}
}