CourseWork/app/Services/TeacherService.php

56 lines
1.3 KiB
PHP
Raw Normal View History

2024-04-23 14:52:49 +04:00
<?php
namespace App\Services;
use App\Models\Teacher;
use App\Models\User;
use Illuminate\Pagination\LengthAwarePaginator;
class TeacherService implements ServiceInterface
{
public function getAll(): LengthAwarePaginator
{
2024-05-06 17:47:25 +04:00
return Teacher::filter()->paginate(5)->withQueryString();
2024-04-23 14:52:49 +04:00
}
2024-04-27 15:10:21 +04:00
public function create(array $data): Teacher
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'],
]);
$teacher = Teacher::create([
'name' => $data['name'],
'last_name' => $data['last_name'],
'middle_name' => $data['middle_name'],
'birthday' => $data['birthday'],
]);
2024-04-23 14:52:49 +04:00
$teacher->user()->save($user);
return $teacher;
}
2024-04-27 15:10:21 +04:00
public function update($model, array $data): Teacher
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'],
]);
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
}
}