CourseWork/app/Services/StudentService.php

59 lines
1.4 KiB
PHP
Raw Normal View History

2024-04-23 14:52:49 +04:00
<?php
namespace App\Services;
use App\Models\Student;
2024-04-27 15:10:21 +04:00
use App\Models\Teacher;
2024-04-23 14:52:49 +04:00
use App\Models\User;
use Illuminate\Pagination\LengthAwarePaginator;
class StudentService implements ServiceInterface
{
public function getAll(): LengthAwarePaginator
{
return Student::filter()->paginate(10)->withQueryString();
}
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);
return $student;
}
2024-04-27 15:10:21 +04:00
public function update($model, array $data): Student
2024-04-23 14:52:49 +04:00
{
2024-04-27 15:10:21 +04:00
$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'],
]);
2024-04-23 14:52:49 +04:00
return $model;
}
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
}
}