CourseWork/app/Services/StudentService.php
m.zargarov 65c47fe151 Fixed
2024-05-27 18:40:22 +04:00

62 lines
1.6 KiB
PHP

<?php
namespace App\Services;
use App\Enums\ScoreEnum;
use App\Models\Student;
use App\Models\User;
use Illuminate\Pagination\LengthAwarePaginator;
class StudentService implements ServiceInterface
{
public function getAll(): LengthAwarePaginator
{
return Student::filter()->paginate(5)->withQueryString();
}
public function create(array $data): Student
{
$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'],
]);
$student->user()->save($user);
$student
->lessons()
->syncWithPivotValues($student->grade->lessons->pluck('id')->all(), ['score' => ScoreEnum::WithoutScore]);
return $student;
}
public function update($model, array $data): Student
{
$model->user()->update([
'email' => $data['email'],
'password' => $data['password'],
]);
$model->update([
'name' => $data['name'],
'last_name' => $data['last_name'],
'middle_name' => $data['middle_name'],
'birthday' => $data['birthday'],
'grade_id' => $data['grade_id'],
]);
return $model;
}
public function delete($model): void
{
$model->user()->delete();
$model->delete();
}
}