From 67a02034f14a3dd3220c93cdb722a0cbd7129125 Mon Sep 17 00:00:00 2001 From: "m.zargarov" Date: Mon, 6 May 2024 17:47:25 +0400 Subject: [PATCH 1/5] Added binding teacher with subjects --- app/Http/Controllers/GradeController.php | 10 +-- app/Http/Controllers/StudentController.php | 10 +-- app/Http/Controllers/SubjectController.php | 10 +-- .../Controllers/SubjectTeacherController.php | 50 +++++++++++ app/Http/Controllers/TeacherController.php | 11 +-- app/Http/Requests/SubjectPostRequest.php | 2 +- .../Requests/SubjectTeacherPostRequest.php | 28 +++++++ app/Providers/ModelServiceProvider.php | 8 ++ app/Services/GradeService.php | 5 +- app/Services/ServiceInterface.php | 8 +- app/Services/StudentService.php | 3 +- app/Services/SubjectService.php | 7 +- app/Services/SubjectTeacherService.php | 43 ++++++++++ app/Services/TeacherService.php | 2 +- resources/views/grades/index.blade.php | 15 ++-- resources/views/students/edit.blade.php | 2 +- resources/views/students/form.blade.php | 4 +- resources/views/students/index.blade.php | 83 +++++++++---------- .../views/subject-teacher/create.blade.php | 5 ++ .../views/subject-teacher/edit.blade.php | 8 ++ .../views/subject-teacher/form.blade.php | 24 ++++++ resources/views/subjects/index.blade.php | 83 +++++++++---------- resources/views/teachers/edit.blade.php | 2 +- resources/views/teachers/form.blade.php | 4 +- resources/views/teachers/index.blade.php | 15 ++-- resources/views/teachers/show.blade.php | 47 ++++++++++- routes/web.php | 13 ++- 27 files changed, 360 insertions(+), 142 deletions(-) create mode 100644 app/Http/Controllers/SubjectTeacherController.php create mode 100644 app/Http/Requests/SubjectTeacherPostRequest.php create mode 100644 app/Services/SubjectTeacherService.php create mode 100644 resources/views/subject-teacher/create.blade.php create mode 100644 resources/views/subject-teacher/edit.blade.php create mode 100644 resources/views/subject-teacher/form.blade.php diff --git a/app/Http/Controllers/GradeController.php b/app/Http/Controllers/GradeController.php index 1aee597..f5f5df3 100644 --- a/app/Http/Controllers/GradeController.php +++ b/app/Http/Controllers/GradeController.php @@ -11,7 +11,7 @@ use Illuminate\View\View; class GradeController extends Controller { public function __construct( - protected ServiceInterface $gradeService, + protected ServiceInterface $service, ){} /** @@ -20,7 +20,7 @@ class GradeController extends Controller public function index(): View { return view('grades.index', [ - 'grades' => $this->gradeService->getAll(), + 'grades' => $this->service->getAll(), ]); } @@ -37,7 +37,7 @@ class GradeController extends Controller */ public function store(GradePostRequest $request): RedirectResponse { - return redirect()->route('grades.show', $this->gradeService->create($request->validated())); + return redirect()->route('grades.show', $this->service->create($request->validated())); } /** @@ -65,7 +65,7 @@ class GradeController extends Controller */ public function update(GradePostRequest $request, Grade $grade): RedirectResponse { - return redirect()->route('grades.show', $this->gradeService->update($grade, $request->validated())); + return redirect()->route('grades.show', $this->service->update($grade, $request->validated())); } /** @@ -73,7 +73,7 @@ class GradeController extends Controller */ public function destroy(Grade $grade): RedirectResponse { - $this->gradeService->delete($grade); + $this->service->delete($grade); return redirect()->route('grades.index'); } diff --git a/app/Http/Controllers/StudentController.php b/app/Http/Controllers/StudentController.php index db8f8a3..d8fd103 100644 --- a/app/Http/Controllers/StudentController.php +++ b/app/Http/Controllers/StudentController.php @@ -12,7 +12,7 @@ use Illuminate\View\View; class StudentController extends Controller { public function __construct( - protected ServiceInterface $studentService + protected ServiceInterface $service ){} /** @@ -21,7 +21,7 @@ class StudentController extends Controller public function index(): View { return view('students.index', [ - 'students' => $this->studentService->getAll(), + 'students' => $this->service->getAll(), ]); } @@ -42,7 +42,7 @@ class StudentController extends Controller { return redirect()->route( 'students.show', - $this->studentService->create($request->validated()) + $this->service->create($request->validated()) ); } @@ -75,7 +75,7 @@ class StudentController extends Controller { return redirect()->route( 'students.show', - $this->studentService->update($student, $request->validated()) + $this->service->update($student, $request->validated()) ); } @@ -84,7 +84,7 @@ class StudentController extends Controller */ public function destroy(Student $student): RedirectResponse { - $this->studentService->delete($student); + $this->service->delete($student); return redirect()->route('students.index'); } diff --git a/app/Http/Controllers/SubjectController.php b/app/Http/Controllers/SubjectController.php index e47975d..a8911b3 100644 --- a/app/Http/Controllers/SubjectController.php +++ b/app/Http/Controllers/SubjectController.php @@ -11,7 +11,7 @@ use Illuminate\View\View; class SubjectController extends Controller { public function __construct( - protected ServiceInterface $subjectService, + protected ServiceInterface $service, ){} /** @@ -20,7 +20,7 @@ class SubjectController extends Controller public function index(): View { return view('subjects.index', [ - "subjects" => $this->subjectService->getAll(), + "subjects" => $this->service->getAll(), ]); } @@ -39,7 +39,7 @@ class SubjectController extends Controller { return redirect()->route( 'subjects.show', - $this->subjectService->create($request->validated()) + $this->service->create($request->validated()) ); } @@ -70,7 +70,7 @@ class SubjectController extends Controller { return redirect()->route( 'subjects.show', - $this->subjectService->update($subject, $request->validated()) + $this->service->update($subject, $request->validated()) ); } @@ -79,7 +79,7 @@ class SubjectController extends Controller */ public function destroy(Subject $subject): RedirectResponse { - $this->subjectService->delete($subject); + $this->service->delete($subject); return redirect()->route('subjects.index'); } diff --git a/app/Http/Controllers/SubjectTeacherController.php b/app/Http/Controllers/SubjectTeacherController.php new file mode 100644 index 0000000..609c773 --- /dev/null +++ b/app/Http/Controllers/SubjectTeacherController.php @@ -0,0 +1,50 @@ + $teacher, + 'subjects' => $this->service->getAllSubjects(), + ]); + } + + public function store(SubjectTeacherPostRequest $request, Teacher $teacher): RedirectResponse + { + return redirect()->route('teachers.show', $this->service->create($request->validated(), $teacher)); + } + + public function edit(Teacher $teacher, Subject $subject): View + { + return view('subject-teacher.edit', [ + 'teacher' => $teacher, + 'updateSubject' => $subject, + 'subjects' => $this->service->getAllSubjects(), + ]); + } + + public function update(SubjectTeacherPostRequest $request, Teacher $teacher, Subject $subject): RedirectResponse + { + return redirect()->route('teachers.show', + $this->service->update($teacher, $request->validated(), $subject)); + } + + public function destroy(Teacher $teacher, Subject $subject): RedirectResponse + { + return redirect()->route('teachers.show', $this->service->delete($teacher, $subject)); + } +} diff --git a/app/Http/Controllers/TeacherController.php b/app/Http/Controllers/TeacherController.php index d6b5f66..1a18cce 100644 --- a/app/Http/Controllers/TeacherController.php +++ b/app/Http/Controllers/TeacherController.php @@ -11,7 +11,7 @@ use Illuminate\View\View; class TeacherController extends Controller { public function __construct( - protected ServiceInterface $teacherService + protected ServiceInterface $service ){} /** @@ -20,7 +20,7 @@ class TeacherController extends Controller public function index(): View { return view('teachers.index', [ - 'teachers' => $this->teacherService->getAll(), + 'teachers' => $this->service->getAll(), ]); } @@ -39,7 +39,7 @@ class TeacherController extends Controller { return redirect()->route( 'teachers.show', - $this->teacherService->create($request->validated()) + $this->service->create($request->validated()) ); } @@ -50,6 +50,7 @@ class TeacherController extends Controller { return view('teachers.show', [ 'teacher' => $teacher, + 'subjects' => $teacher->subjects, ]); } @@ -70,7 +71,7 @@ class TeacherController extends Controller { return redirect()->route( 'teachers.show', - $this->teacherService->update($teacher, $request->validated()) + $this->service->update($teacher, $request->validated()) ); } @@ -79,7 +80,7 @@ class TeacherController extends Controller */ public function destroy(Teacher $teacher): RedirectResponse { - $this->teacherService->delete($teacher); + $this->service->delete($teacher); return redirect()->route('teachers.index'); } diff --git a/app/Http/Requests/SubjectPostRequest.php b/app/Http/Requests/SubjectPostRequest.php index 6a97968..6166d9d 100644 --- a/app/Http/Requests/SubjectPostRequest.php +++ b/app/Http/Requests/SubjectPostRequest.php @@ -22,7 +22,7 @@ class SubjectPostRequest extends FormRequest public function rules(): array { return [ - 'name' => 'required|unique:grades|max:255', + 'name' => 'required|unique:subjects|max:255', ]; } } diff --git a/app/Http/Requests/SubjectTeacherPostRequest.php b/app/Http/Requests/SubjectTeacherPostRequest.php new file mode 100644 index 0000000..68e09c8 --- /dev/null +++ b/app/Http/Requests/SubjectTeacherPostRequest.php @@ -0,0 +1,28 @@ +|string> + */ + public function rules(): array + { + return [ + 'subject_id' => 'required|exists:subjects,id', + ]; + } +} diff --git a/app/Providers/ModelServiceProvider.php b/app/Providers/ModelServiceProvider.php index cfbd547..191b6f8 100644 --- a/app/Providers/ModelServiceProvider.php +++ b/app/Providers/ModelServiceProvider.php @@ -5,11 +5,13 @@ namespace App\Providers; use App\Http\Controllers\GradeController; use App\Http\Controllers\StudentController; use App\Http\Controllers\SubjectController; +use App\Http\Controllers\SubjectTeacherController; use App\Http\Controllers\TeacherController; use App\Services\GradeService; use App\Services\ServiceInterface; use App\Services\StudentService; use App\Services\SubjectService; +use App\Services\SubjectTeacherService; use App\Services\TeacherService; use Illuminate\Support\ServiceProvider; @@ -43,6 +45,12 @@ class ModelServiceProvider extends ServiceProvider ->give(function () { return new SubjectService(); }); + + $this->app->when(SubjectTeacherController::class) + ->needs(ServiceInterface::class) + ->give(function () { + return new SubjectTeacherService(); + }); } /** diff --git a/app/Services/GradeService.php b/app/Services/GradeService.php index b59e22f..38ad047 100644 --- a/app/Services/GradeService.php +++ b/app/Services/GradeService.php @@ -3,6 +3,7 @@ namespace App\Services; use App\Models\Grade; +use Illuminate\Database\Eloquent\Model; use Illuminate\Pagination\LengthAwarePaginator; class GradeService implements ServiceInterface @@ -10,7 +11,7 @@ class GradeService implements ServiceInterface public function getAll(): LengthAwarePaginator { - return Grade::filter()->paginate(10)->withQueryString(); + return Grade::filter()->paginate(5)->withQueryString(); } public function create(array $data): Grade @@ -18,7 +19,7 @@ class GradeService implements ServiceInterface return Grade::create($data); } - public function update($model, array $data): Grade + public function update(Model $model, array $data): Grade { $model->update($data); diff --git a/app/Services/ServiceInterface.php b/app/Services/ServiceInterface.php index bcc6856..ef9a45c 100644 --- a/app/Services/ServiceInterface.php +++ b/app/Services/ServiceInterface.php @@ -2,13 +2,15 @@ namespace App\Services; +use Illuminate\Database\Eloquent\Model; + interface ServiceInterface { public function getAll(); - public function create(array $data); + public function create(array $data): Model; - public function update($model, array $data); + public function update(Model $model, array $data): Model; - public function delete($model); + public function delete(Model $model); } diff --git a/app/Services/StudentService.php b/app/Services/StudentService.php index ea92863..ac4d0b0 100644 --- a/app/Services/StudentService.php +++ b/app/Services/StudentService.php @@ -5,13 +5,14 @@ namespace App\Services; use App\Models\Student; use App\Models\Teacher; use App\Models\User; +use Illuminate\Database\Eloquent\Model; use Illuminate\Pagination\LengthAwarePaginator; class StudentService implements ServiceInterface { public function getAll(): LengthAwarePaginator { - return Student::filter()->paginate(10)->withQueryString(); + return Student::filter()->paginate(5)->withQueryString(); } public function create(array $data): Student diff --git a/app/Services/SubjectService.php b/app/Services/SubjectService.php index 2d22384..c857584 100644 --- a/app/Services/SubjectService.php +++ b/app/Services/SubjectService.php @@ -3,6 +3,7 @@ namespace App\Services; use App\Models\Subject; +use Illuminate\Database\Eloquent\Model; use Illuminate\Pagination\LengthAwarePaginator; class SubjectService implements ServiceInterface @@ -10,7 +11,7 @@ class SubjectService implements ServiceInterface public function getAll(): LengthAwarePaginator { - return Subject::filter()->paginate(10)->withQueryString(); + return Subject::filter()->paginate(5)->withQueryString(); } public function create(array $data): Subject @@ -18,14 +19,14 @@ class SubjectService implements ServiceInterface return Subject::create($data); } - public function update($model, array $data): Subject + public function update(Model $model, array $data): Subject { $model->update($data); return $model; } - public function delete($model): void + public function delete(Model $model): void { $model->delete(); } diff --git a/app/Services/SubjectTeacherService.php b/app/Services/SubjectTeacherService.php new file mode 100644 index 0000000..c0dbe93 --- /dev/null +++ b/app/Services/SubjectTeacherService.php @@ -0,0 +1,43 @@ +subjects; + } + + public function getAllSubjects(): Collection + { + return Subject::all(); + } + + public function create(array $data, Model $model = null): Teacher + { + $model->subjects()->syncWithoutDetaching($data['subject_id']); + + return $model; + } + + public function update(Model $model, array $data, Model $subject = null): Teacher + { + $model->subjects()->detach($subject->id); + $model->subjects()->attach($data['subject_id']); + + return $model; + } + + public function delete(Model $model, Model $subject = null): Teacher + { + $model->subjects()->detach($subject); + + return $model; + } +} diff --git a/app/Services/TeacherService.php b/app/Services/TeacherService.php index 46573d1..374c995 100644 --- a/app/Services/TeacherService.php +++ b/app/Services/TeacherService.php @@ -11,7 +11,7 @@ class TeacherService implements ServiceInterface { public function getAll(): LengthAwarePaginator { - return Teacher::filter()->paginate(2)->withQueryString(); + return Teacher::filter()->paginate(5)->withQueryString(); } public function create(array $data): Teacher diff --git a/resources/views/grades/index.blade.php b/resources/views/grades/index.blade.php index ddff5cc..03b8b10 100644 --- a/resources/views/grades/index.blade.php +++ b/resources/views/grades/index.blade.php @@ -3,12 +3,6 @@ @section('content')
-
-
{{__('Новый класс')}}
- -
{{__('Поиск класса')}}
@@ -24,10 +18,10 @@
- @if (count($grades))
{{__('Классы')}}
+ @if (count($grades)) @@ -64,10 +58,15 @@ @endforeach
Название
+ @else +

Классы отсутствуют

+ @endif +
+
- @endif
@endsection diff --git a/resources/views/students/edit.blade.php b/resources/views/students/edit.blade.php index eb50242..2dfa1ba 100644 --- a/resources/views/students/edit.blade.php +++ b/resources/views/students/edit.blade.php @@ -1,5 +1,5 @@ @extends('layouts.app') @section('content') - @include('students.form', ['route' => route('students.update', $user), 'method' => 'PUT']) + @include('students.form', ['route' => route('students.update', $student), 'method' => 'PUT']) @endsection diff --git a/resources/views/students/form.blade.php b/resources/views/students/form.blade.php index 82f694c..69471b5 100644 --- a/resources/views/students/form.blade.php +++ b/resources/views/students/form.blade.php @@ -34,11 +34,11 @@
- +
- +
diff --git a/resources/views/students/index.blade.php b/resources/views/students/index.blade.php index e1ec961..22baac5 100644 --- a/resources/views/students/index.blade.php +++ b/resources/views/students/index.blade.php @@ -3,12 +3,6 @@ @section('content')
-
-
{{__('Новый ученик')}}
- -
{{__('Поиск ученика')}}
@@ -23,43 +17,48 @@
- @if (count($students)) -
-
{{__('Ученики')}}
-
- - - - - - - @foreach ($students as $student) - - - - - - @endforeach - -
ФИО 
- - - - -
- @csrf - @method('DELETE') - -
-
-
- +
+
{{__('Ученики')}}
+
+ @if(count($students)) + + + + + + + @foreach ($students as $student) + + + + + + @endforeach + +
ФИО 
+ + + + +
+ @csrf + @method('DELETE') + +
+
+ @else +

Ученики остутствуют

+ @endif
- @endif + + +
@endsection diff --git a/resources/views/subject-teacher/create.blade.php b/resources/views/subject-teacher/create.blade.php new file mode 100644 index 0000000..b91d9d4 --- /dev/null +++ b/resources/views/subject-teacher/create.blade.php @@ -0,0 +1,5 @@ +@extends('layouts.app') + +@section('content') + @include('subject-teacher.form', ['route' => route('teachers.subjects.store', $teacher), 'method' => 'POST']) +@endsection diff --git a/resources/views/subject-teacher/edit.blade.php b/resources/views/subject-teacher/edit.blade.php new file mode 100644 index 0000000..b124ec1 --- /dev/null +++ b/resources/views/subject-teacher/edit.blade.php @@ -0,0 +1,8 @@ +@extends('layouts.app') + +@section('content') + @include('subject-teacher.form', [ + 'route' => route('teachers.subjects.update', + ['teacher' => $teacher, 'subject' => $updateSubject] + ), 'method' => 'PUT']) +@endsection diff --git a/resources/views/subject-teacher/form.blade.php b/resources/views/subject-teacher/form.blade.php new file mode 100644 index 0000000..819fa69 --- /dev/null +++ b/resources/views/subject-teacher/form.blade.php @@ -0,0 +1,24 @@ +
+
+
+
+
+ @csrf + @method($method) +
+ + +
+ +
+
+
+
+
diff --git a/resources/views/subjects/index.blade.php b/resources/views/subjects/index.blade.php index 53df0be..23074a3 100644 --- a/resources/views/subjects/index.blade.php +++ b/resources/views/subjects/index.blade.php @@ -3,12 +3,6 @@ @section('content')
-
-
{{__('Новый предмет')}}
- -
{{__('Поиск предмета')}}
@@ -23,43 +17,48 @@
- @if (count($subjects)) -
-
{{__('Предметы')}}
-
- - - - - - - @foreach ($subjects as $subject) - - - - - - @endforeach - -
Название 
- - - - -
- @csrf - @method('DELETE') - -
-
-
- +
+
{{__('Предметы')}}
+
+ @if (count($subjects)) + + + + + + + @foreach ($subjects as $subject) + + + + + + @endforeach + +
Название 
+ + + + +
+ @csrf + @method('DELETE') + +
+
+ @else +

Предметы отсутствуют

+ @endif
- @endif + + +
@endsection diff --git a/resources/views/teachers/edit.blade.php b/resources/views/teachers/edit.blade.php index 7b8428a..bfaced9 100644 --- a/resources/views/teachers/edit.blade.php +++ b/resources/views/teachers/edit.blade.php @@ -1,5 +1,5 @@ @extends('layouts.app') @section('content') - @include('teachers.form', ['route' => route('teachers.update', $user), 'method' => 'PUT']) + @include('teachers.form', ['route' => route('teachers.update', $teacher), 'method' => 'PUT']) @endsection diff --git a/resources/views/teachers/form.blade.php b/resources/views/teachers/form.blade.php index 8f27def..f74896c 100644 --- a/resources/views/teachers/form.blade.php +++ b/resources/views/teachers/form.blade.php @@ -23,11 +23,11 @@
- +
- +
diff --git a/resources/views/teachers/index.blade.php b/resources/views/teachers/index.blade.php index 3c3f89c..9507ea1 100644 --- a/resources/views/teachers/index.blade.php +++ b/resources/views/teachers/index.blade.php @@ -3,12 +3,6 @@ @section('content')
-
-
{{__('Новый учитель')}}
- -
{{__('Поиск учителя')}}
@@ -23,10 +17,10 @@
- @if (count($teachers))
{{__('Учителя')}}
+ @if (count($teachers)) @@ -56,10 +50,15 @@ @endforeach
ФИО
+ @else +

Учителя отсутствуют

+ @endif +
+
- @endif
@endsection diff --git a/resources/views/teachers/show.blade.php b/resources/views/teachers/show.blade.php index d3d7b9d..19deb67 100644 --- a/resources/views/teachers/show.blade.php +++ b/resources/views/teachers/show.blade.php @@ -4,7 +4,9 @@
-
{{ ('Учитель') }}
+
+ {{ ('Учитель') }} +
ФИО: {{ $teacher->fio }}
@@ -17,6 +19,49 @@ Редактировать
+
+
+ {{__('Предметы учителя')}} +
+
+ @if(count($subjects)) + + + + + + + @foreach ($subjects as $subject) + + + + + + @endforeach + +
Название 
+
+ {{ $subject->name }} +
+
+ + +
+ @csrf + @method('DELETE') + +
+
+ @else +

У учителя отсутствуют предметы

+ @endif +
+ +
@endsection diff --git a/routes/web.php b/routes/web.php index 6758792..e5f2651 100644 --- a/routes/web.php +++ b/routes/web.php @@ -3,6 +3,7 @@ use App\Http\Controllers\GradeController; use App\Http\Controllers\StudentController; use App\Http\Controllers\SubjectController; +use App\Http\Controllers\SubjectTeacherController; use App\Http\Controllers\TeacherController; use Illuminate\Support\Facades\Route; @@ -10,7 +11,11 @@ Route::get('/', function () { return redirect()->route('grades.index'); }); -Route::resource('grades', GradeController::class); -Route::resource('subjects', SubjectController::class); -Route::resource('students', StudentController::class); -Route::resource('teachers', TeacherController::class); +Route::resources([ + 'grades' => GradeController::class, + 'subjects' => SubjectController::class, + 'students' => StudentController::class, + 'teachers' => TeacherController::class, +]); + +Route::resource('teachers.subjects', SubjectTeacherController::class)->except('index', 'show'); -- 2.25.1 From fb2fda38bb0deb348e2db0aa77a6b6b550e93dc0 Mon Sep 17 00:00:00 2001 From: "m.zargarov" Date: Mon, 6 May 2024 17:51:16 +0400 Subject: [PATCH 2/5] Correction code style --- app/Http/Controllers/GradeController.php | 5 +++-- app/Http/Controllers/StudentController.php | 3 ++- app/Http/Controllers/SubjectController.php | 7 ++++--- app/Http/Controllers/SubjectTeacherController.php | 5 +++-- app/Http/Controllers/TeacherController.php | 3 ++- app/Http/Requests/StudentPostRequest.php | 2 -- app/Http/Requests/TeacherPostRequest.php | 2 -- app/Models/Student.php | 7 ++++--- app/Models/Teacher.php | 7 ++++--- app/Services/GradeService.php | 1 - app/Services/StudentService.php | 2 -- app/Services/SubjectService.php | 1 - app/Services/SubjectTeacherService.php | 8 ++++---- app/Services/TeacherService.php | 1 - 14 files changed, 26 insertions(+), 28 deletions(-) diff --git a/app/Http/Controllers/GradeController.php b/app/Http/Controllers/GradeController.php index f5f5df3..c998e75 100644 --- a/app/Http/Controllers/GradeController.php +++ b/app/Http/Controllers/GradeController.php @@ -12,7 +12,8 @@ class GradeController extends Controller { public function __construct( protected ServiceInterface $service, - ){} + ) { + } /** * Display a listing of the resource. @@ -46,7 +47,7 @@ class GradeController extends Controller public function show(Grade $grade): View { return view('grades.show', [ - 'grade' => $grade, + 'grade' => $grade, ]); } diff --git a/app/Http/Controllers/StudentController.php b/app/Http/Controllers/StudentController.php index d8fd103..fa76501 100644 --- a/app/Http/Controllers/StudentController.php +++ b/app/Http/Controllers/StudentController.php @@ -13,7 +13,8 @@ class StudentController extends Controller { public function __construct( protected ServiceInterface $service - ){} + ) { + } /** * Display a listing of the resource. diff --git a/app/Http/Controllers/SubjectController.php b/app/Http/Controllers/SubjectController.php index a8911b3..58e670f 100644 --- a/app/Http/Controllers/SubjectController.php +++ b/app/Http/Controllers/SubjectController.php @@ -12,7 +12,8 @@ class SubjectController extends Controller { public function __construct( protected ServiceInterface $service, - ){} + ) { + } /** * Display a listing of the resource. @@ -20,7 +21,7 @@ class SubjectController extends Controller public function index(): View { return view('subjects.index', [ - "subjects" => $this->service->getAll(), + 'subjects' => $this->service->getAll(), ]); } @@ -49,7 +50,7 @@ class SubjectController extends Controller public function show(Subject $subject): View { return view('subjects.show', [ - "subject" => $subject + 'subject' => $subject, ]); } diff --git a/app/Http/Controllers/SubjectTeacherController.php b/app/Http/Controllers/SubjectTeacherController.php index 609c773..1cb6fcc 100644 --- a/app/Http/Controllers/SubjectTeacherController.php +++ b/app/Http/Controllers/SubjectTeacherController.php @@ -13,7 +13,8 @@ class SubjectTeacherController extends Controller { public function __construct( protected ServiceInterface $service, - ){} + ) { + } public function create(Teacher $teacher): View { @@ -25,7 +26,7 @@ class SubjectTeacherController extends Controller public function store(SubjectTeacherPostRequest $request, Teacher $teacher): RedirectResponse { - return redirect()->route('teachers.show', $this->service->create($request->validated(), $teacher)); + return redirect()->route('teachers.show', $this->service->create($request->validated(), $teacher)); } public function edit(Teacher $teacher, Subject $subject): View diff --git a/app/Http/Controllers/TeacherController.php b/app/Http/Controllers/TeacherController.php index 1a18cce..f82ffda 100644 --- a/app/Http/Controllers/TeacherController.php +++ b/app/Http/Controllers/TeacherController.php @@ -12,7 +12,8 @@ class TeacherController extends Controller { public function __construct( protected ServiceInterface $service - ){} + ) { + } /** * Display a listing of the resource. diff --git a/app/Http/Requests/StudentPostRequest.php b/app/Http/Requests/StudentPostRequest.php index 71bc504..132c34d 100644 --- a/app/Http/Requests/StudentPostRequest.php +++ b/app/Http/Requests/StudentPostRequest.php @@ -2,8 +2,6 @@ namespace App\Http\Requests; -use App\Enums\RoleEnum; -use Illuminate\Contracts\Validation\ValidationRule; use Illuminate\Foundation\Http\FormRequest; class StudentPostRequest extends FormRequest diff --git a/app/Http/Requests/TeacherPostRequest.php b/app/Http/Requests/TeacherPostRequest.php index 932d7d5..e3aea8a 100644 --- a/app/Http/Requests/TeacherPostRequest.php +++ b/app/Http/Requests/TeacherPostRequest.php @@ -2,8 +2,6 @@ namespace App\Http\Requests; -use App\Enums\RoleEnum; -use Illuminate\Contracts\Validation\ValidationRule; use Illuminate\Foundation\Http\FormRequest; class TeacherPostRequest extends FormRequest diff --git a/app/Models/Student.php b/app/Models/Student.php index 7e97ab8..43844ae 100644 --- a/app/Models/Student.php +++ b/app/Models/Student.php @@ -2,12 +2,13 @@ namespace App\Models; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\MorphOne; -use Illuminate\Database\Eloquent\Builder; + class Student extends Model { use HasFactory; @@ -33,7 +34,7 @@ class Student extends Model public function scopeFilter(Builder $query): void { $name = request('name'); - $query->when($name, function (Builder $query, $name){ + $query->when($name, function (Builder $query, $name) { $query->whereRaw('CONCAT (name, \' \', surname, \' \', patronymic) ilike ?', ["$name%"]); $query->orWhereRaw('CONCAT (name, \' \', patronymic, \' \', surname) ilike ?', ["$name%"]); $query->orWhereRaw('CONCAT (surname, \' \', name, \' \', patronymic) ilike ?', ["$name%"]); @@ -46,7 +47,7 @@ class Student extends Model public function fio(): Attribute { return Attribute::make( - get: fn () => $this->last_name . " " . $this->name . " " . $this->middle_name, + get: fn () => $this->last_name.' '.$this->name.' '.$this->middle_name, ); } } diff --git a/app/Models/Teacher.php b/app/Models/Teacher.php index c5cbf24..f18c015 100644 --- a/app/Models/Teacher.php +++ b/app/Models/Teacher.php @@ -2,12 +2,13 @@ namespace App\Models; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\MorphOne; -use Illuminate\Database\Eloquent\Builder; + class Teacher extends Model { use HasFactory; @@ -37,7 +38,7 @@ class Teacher extends Model public function scopeFilter(Builder $query): void { $name = request('name'); - $query->when($name, function (Builder $query, $name){ + $query->when($name, function (Builder $query, $name) { $query->whereRaw('CONCAT (name, \' \', surname, \' \', patronymic) ilike ?', ["$name%"]); $query->orWhereRaw('CONCAT (name, \' \', patronymic, \' \', surname) ilike ?', ["$name%"]); $query->orWhereRaw('CONCAT (surname, \' \', name, \' \', patronymic) ilike ?', ["$name%"]); @@ -50,7 +51,7 @@ class Teacher extends Model public function fio(): Attribute { return Attribute::make( - get: fn () => $this->last_name . " " . $this->name . " " . $this->middle_name, + get: fn () => $this->last_name.' '.$this->name.' '.$this->middle_name, ); } } diff --git a/app/Services/GradeService.php b/app/Services/GradeService.php index 38ad047..6ab6453 100644 --- a/app/Services/GradeService.php +++ b/app/Services/GradeService.php @@ -8,7 +8,6 @@ use Illuminate\Pagination\LengthAwarePaginator; class GradeService implements ServiceInterface { - public function getAll(): LengthAwarePaginator { return Grade::filter()->paginate(5)->withQueryString(); diff --git a/app/Services/StudentService.php b/app/Services/StudentService.php index ac4d0b0..6aafb74 100644 --- a/app/Services/StudentService.php +++ b/app/Services/StudentService.php @@ -3,9 +3,7 @@ namespace App\Services; use App\Models\Student; -use App\Models\Teacher; use App\Models\User; -use Illuminate\Database\Eloquent\Model; use Illuminate\Pagination\LengthAwarePaginator; class StudentService implements ServiceInterface diff --git a/app/Services/SubjectService.php b/app/Services/SubjectService.php index c857584..6687465 100644 --- a/app/Services/SubjectService.php +++ b/app/Services/SubjectService.php @@ -8,7 +8,6 @@ use Illuminate\Pagination\LengthAwarePaginator; class SubjectService implements ServiceInterface { - public function getAll(): LengthAwarePaginator { return Subject::filter()->paginate(5)->withQueryString(); diff --git a/app/Services/SubjectTeacherService.php b/app/Services/SubjectTeacherService.php index c0dbe93..d5ea630 100644 --- a/app/Services/SubjectTeacherService.php +++ b/app/Services/SubjectTeacherService.php @@ -9,7 +9,7 @@ use Illuminate\Database\Eloquent\Model; class SubjectTeacherService implements ServiceInterface { - public function getAll(Teacher $teacher = null): Collection + public function getAll(?Teacher $teacher = null): Collection { return $teacher->subjects; } @@ -19,14 +19,14 @@ class SubjectTeacherService implements ServiceInterface return Subject::all(); } - public function create(array $data, Model $model = null): Teacher + public function create(array $data, ?Model $model = null): Teacher { $model->subjects()->syncWithoutDetaching($data['subject_id']); return $model; } - public function update(Model $model, array $data, Model $subject = null): Teacher + public function update(Model $model, array $data, ?Model $subject = null): Teacher { $model->subjects()->detach($subject->id); $model->subjects()->attach($data['subject_id']); @@ -34,7 +34,7 @@ class SubjectTeacherService implements ServiceInterface return $model; } - public function delete(Model $model, Model $subject = null): Teacher + public function delete(Model $model, ?Model $subject = null): Teacher { $model->subjects()->detach($subject); diff --git a/app/Services/TeacherService.php b/app/Services/TeacherService.php index 374c995..ea04740 100644 --- a/app/Services/TeacherService.php +++ b/app/Services/TeacherService.php @@ -2,7 +2,6 @@ namespace App\Services; -use App\Models\Student; use App\Models\Teacher; use App\Models\User; use Illuminate\Pagination\LengthAwarePaginator; -- 2.25.1 From 622b359e1f4365bc5037bbf6e7197248b87eea76 Mon Sep 17 00:00:00 2001 From: "m.zargarov" Date: Tue, 7 May 2024 12:14:18 +0400 Subject: [PATCH 3/5] Added binding grade with subjects --- app/Http/Controllers/GradeController.php | 1 + .../Controllers/GradeSubjectController.php | 51 +++++++++++++++++++ app/Http/Requests/GradeSubjectPostRequest.php | 28 ++++++++++ app/Providers/ModelServiceProvider.php | 8 +++ app/Services/GradeSubjectService.php | 44 ++++++++++++++++ .../views/grade-subject/create.blade.php | 5 ++ resources/views/grade-subject/edit.blade.php | 8 +++ resources/views/grade-subject/form.blade.php | 24 +++++++++ resources/views/grades/index.blade.php | 2 +- resources/views/grades/show.blade.php | 43 ++++++++++++++++ resources/views/students/index.blade.php | 2 +- resources/views/subjects/index.blade.php | 2 +- resources/views/teachers/index.blade.php | 2 +- resources/views/teachers/show.blade.php | 2 +- routes/web.php | 2 + 15 files changed, 219 insertions(+), 5 deletions(-) create mode 100644 app/Http/Controllers/GradeSubjectController.php create mode 100644 app/Http/Requests/GradeSubjectPostRequest.php create mode 100644 app/Services/GradeSubjectService.php create mode 100644 resources/views/grade-subject/create.blade.php create mode 100644 resources/views/grade-subject/edit.blade.php create mode 100644 resources/views/grade-subject/form.blade.php diff --git a/app/Http/Controllers/GradeController.php b/app/Http/Controllers/GradeController.php index c998e75..4a60756 100644 --- a/app/Http/Controllers/GradeController.php +++ b/app/Http/Controllers/GradeController.php @@ -48,6 +48,7 @@ class GradeController extends Controller { return view('grades.show', [ 'grade' => $grade, + 'subjects' => $grade->subjects, ]); } diff --git a/app/Http/Controllers/GradeSubjectController.php b/app/Http/Controllers/GradeSubjectController.php new file mode 100644 index 0000000..aeaaa0f --- /dev/null +++ b/app/Http/Controllers/GradeSubjectController.php @@ -0,0 +1,51 @@ + $grade, + 'subjects' => $this->service->getAllSubjects(), + ]); + } + + public function store(GradeSubjectPostRequest $request, Grade $grade): RedirectResponse + { + return redirect()->route('grades.show', $this->service->create($request->validated(), $grade)); + } + + public function edit(Grade $grade, Subject $subject): View + { + return view('grade-subject.edit', [ + 'grade' => $grade, + 'updateSubject' => $subject, + 'subjects' => $this->service->getAllSubjects(), + ]); + } + + public function update(GradeSubjectPostRequest $request, Grade $grade, Subject $subject): RedirectResponse + { + return redirect()->route('grades.show', + $this->service->update($grade, $request->validated(), $subject)); + } + + public function destroy(Grade $grade, Subject $subject): RedirectResponse + { + return redirect()->route('grades.show', $this->service->delete($grade, $subject)); + } +} diff --git a/app/Http/Requests/GradeSubjectPostRequest.php b/app/Http/Requests/GradeSubjectPostRequest.php new file mode 100644 index 0000000..4fe34da --- /dev/null +++ b/app/Http/Requests/GradeSubjectPostRequest.php @@ -0,0 +1,28 @@ +|string> + */ + public function rules(): array + { + return [ + 'subject_id' => 'required|exists:subjects,id', + ]; + } +} diff --git a/app/Providers/ModelServiceProvider.php b/app/Providers/ModelServiceProvider.php index 191b6f8..72587d4 100644 --- a/app/Providers/ModelServiceProvider.php +++ b/app/Providers/ModelServiceProvider.php @@ -3,11 +3,13 @@ namespace App\Providers; use App\Http\Controllers\GradeController; +use App\Http\Controllers\GradeSubjectController; use App\Http\Controllers\StudentController; use App\Http\Controllers\SubjectController; use App\Http\Controllers\SubjectTeacherController; use App\Http\Controllers\TeacherController; use App\Services\GradeService; +use App\Services\GradeSubjectService; use App\Services\ServiceInterface; use App\Services\StudentService; use App\Services\SubjectService; @@ -51,6 +53,12 @@ class ModelServiceProvider extends ServiceProvider ->give(function () { return new SubjectTeacherService(); }); + + $this->app->when(GradeSubjectController::class) + ->needs(ServiceInterface::class) + ->give(function () { + return new GradeSubjectService(); + }); } /** diff --git a/app/Services/GradeSubjectService.php b/app/Services/GradeSubjectService.php new file mode 100644 index 0000000..0211393 --- /dev/null +++ b/app/Services/GradeSubjectService.php @@ -0,0 +1,44 @@ +subjects; + } + + public function getAllSubjects(): Collection + { + return Subject::all(); + } + + public function create(array $data, ?Model $model = null): Grade + { + $model->subjects()->syncWithoutDetaching($data['subject_id']); + + return $model; + } + + public function update(Model $model, array $data, ?Model $subject = null): Grade + { + $model->subjects()->detach($subject->id); + $model->subjects()->attach($data['subject_id']); + + return $model; + } + + public function delete(Model $model, ?Model $subject = null): Grade + { + $model->subjects()->detach($subject); + + return $model; + } +} diff --git a/resources/views/grade-subject/create.blade.php b/resources/views/grade-subject/create.blade.php new file mode 100644 index 0000000..7777bc5 --- /dev/null +++ b/resources/views/grade-subject/create.blade.php @@ -0,0 +1,5 @@ +@extends('layouts.app') + +@section('content') + @include('grade-subject.form', ['route' => route('grades.subjects.store', $grade), 'method' => 'POST']) +@endsection diff --git a/resources/views/grade-subject/edit.blade.php b/resources/views/grade-subject/edit.blade.php new file mode 100644 index 0000000..2c4c73c --- /dev/null +++ b/resources/views/grade-subject/edit.blade.php @@ -0,0 +1,8 @@ +@extends('layouts.app') + +@section('content') + @include('grade-subject.form', [ + 'route' => route('grades.subjects.update', + ['grade' => $grade, 'subject' => $updateSubject] + ), 'method' => 'PUT']) +@endsection diff --git a/resources/views/grade-subject/form.blade.php b/resources/views/grade-subject/form.blade.php new file mode 100644 index 0000000..819fa69 --- /dev/null +++ b/resources/views/grade-subject/form.blade.php @@ -0,0 +1,24 @@ +
+
+
+
+
+ @csrf + @method($method) +
+ + +
+ +
+
+
+
+
diff --git a/resources/views/grades/index.blade.php b/resources/views/grades/index.blade.php index 03b8b10..a2dffb7 100644 --- a/resources/views/grades/index.blade.php +++ b/resources/views/grades/index.blade.php @@ -62,7 +62,7 @@

Классы отсутствуют

@endif
-
+ diff --git a/resources/views/grades/show.blade.php b/resources/views/grades/show.blade.php index cfa51d7..0370dbe 100644 --- a/resources/views/grades/show.blade.php +++ b/resources/views/grades/show.blade.php @@ -14,6 +14,49 @@ Редактировать
+
+
+ {{__('Журнал предметов')}} +
+
+ @if(count($subjects)) + + + + + + + @foreach ($subjects as $subject) + + + + + + @endforeach + +
Название 
+
+ {{ $subject->name }} +
+
+ + +
+ @csrf + @method('DELETE') + +
+
+ @else +

У класса отсутствуют предметы

+ @endif +
+ +
@endsection diff --git a/resources/views/students/index.blade.php b/resources/views/students/index.blade.php index 22baac5..822b81a 100644 --- a/resources/views/students/index.blade.php +++ b/resources/views/students/index.blade.php @@ -54,7 +54,7 @@

Ученики остутствуют

@endif -
+ diff --git a/resources/views/subjects/index.blade.php b/resources/views/subjects/index.blade.php index 23074a3..9e45ef4 100644 --- a/resources/views/subjects/index.blade.php +++ b/resources/views/subjects/index.blade.php @@ -54,7 +54,7 @@

Предметы отсутствуют

@endif
-
+ diff --git a/resources/views/teachers/index.blade.php b/resources/views/teachers/index.blade.php index 9507ea1..4aeaae7 100644 --- a/resources/views/teachers/index.blade.php +++ b/resources/views/teachers/index.blade.php @@ -54,7 +54,7 @@

Учителя отсутствуют

@endif
-
+ diff --git a/resources/views/teachers/show.blade.php b/resources/views/teachers/show.blade.php index 19deb67..8f7cc2e 100644 --- a/resources/views/teachers/show.blade.php +++ b/resources/views/teachers/show.blade.php @@ -58,7 +58,7 @@

У учителя отсутствуют предметы

@endif
-
+
diff --git a/routes/web.php b/routes/web.php index e5f2651..8d721f1 100644 --- a/routes/web.php +++ b/routes/web.php @@ -1,6 +1,7 @@ except('index', 'show'); +Route::resource('grades.subjects', GradeSubjectController::class)->except('index', 'show'); -- 2.25.1 From 2ccf102e5e8329ea7276a4ed83974e6c11ae42a7 Mon Sep 17 00:00:00 2001 From: "m.zargarov" Date: Tue, 7 May 2024 12:18:02 +0400 Subject: [PATCH 4/5] Correction code style --- app/Services/GradeSubjectService.php | 1 - 1 file changed, 1 deletion(-) diff --git a/app/Services/GradeSubjectService.php b/app/Services/GradeSubjectService.php index 0211393..55d4793 100644 --- a/app/Services/GradeSubjectService.php +++ b/app/Services/GradeSubjectService.php @@ -4,7 +4,6 @@ namespace App\Services; use App\Models\Grade; use App\Models\Subject; -use App\Models\Teacher; use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Model; -- 2.25.1 From 2b7f8cf2df12403b69cd7b2e1ee71ecbe5eee60f Mon Sep 17 00:00:00 2001 From: "m.zargarov" Date: Tue, 7 May 2024 15:05:10 +0400 Subject: [PATCH 5/5] Added binding grade with teachers --- .../Controllers/GradeTeacherController.php | 62 +++++++++++++++++++ .../Controllers/SubjectTeacherController.php | 9 +++ app/Http/Requests/GradeTeacherPostRequest.php | 28 +++++++++ app/Providers/ModelServiceProvider.php | 8 +++ app/Services/GradeTeacherService.php | 47 ++++++++++++++ app/Services/SubjectTeacherService.php | 8 +++ .../views/grade-teacher/create.blade.php | 5 ++ resources/views/grade-teacher/edit.blade.php | 8 +++ resources/views/grade-teacher/form.blade.php | 27 ++++++++ .../views/subject-teacher/show.blade.php | 52 ++++++++++++++++ resources/views/teachers/show.blade.php | 2 +- routes/web.php | 4 +- 12 files changed, 258 insertions(+), 2 deletions(-) create mode 100644 app/Http/Controllers/GradeTeacherController.php create mode 100644 app/Http/Requests/GradeTeacherPostRequest.php create mode 100644 app/Services/GradeTeacherService.php create mode 100644 resources/views/grade-teacher/create.blade.php create mode 100644 resources/views/grade-teacher/edit.blade.php create mode 100644 resources/views/grade-teacher/form.blade.php create mode 100644 resources/views/subject-teacher/show.blade.php diff --git a/app/Http/Controllers/GradeTeacherController.php b/app/Http/Controllers/GradeTeacherController.php new file mode 100644 index 0000000..735d459 --- /dev/null +++ b/app/Http/Controllers/GradeTeacherController.php @@ -0,0 +1,62 @@ + $teacher, + 'subject' => $subject, + 'grades' => $this->service->getGrades($subject), + ]); + } + + public function store(GradeTeacherPostRequest $request, Teacher $teacher, Subject $subject): RedirectResponse + { + return redirect()->route('teachers.subjects.show', [ + $this->service->create($request->validated(), $teacher), + $subject, + ]); + } + + public function edit(Teacher $teacher, Subject $subject, Grade $grade): View + { + return view('grade-teacher.edit', [ + 'teacher' => $teacher, + 'subject' => $subject, + 'updateGrade' => $grade, + 'grades' => $this->service->getGrades($subject), + ]); + } + + public function update(GradeTeacherPostRequest $request, Teacher $teacher, Subject $subject, Grade $grade): RedirectResponse + { + return redirect()->route('teachers.subjects.show', [ + $this->service->update($teacher, $request->validated(), $grade), + $subject, + ]); + } + + public function destroy(Teacher $teacher, Subject $subject, Grade $grade): RedirectResponse + { + return redirect()->route('teachers.subjects.show', [ + $this->service->delete($teacher, $grade), + $subject, + ]); + } +} diff --git a/app/Http/Controllers/SubjectTeacherController.php b/app/Http/Controllers/SubjectTeacherController.php index 1cb6fcc..eb8db9a 100644 --- a/app/Http/Controllers/SubjectTeacherController.php +++ b/app/Http/Controllers/SubjectTeacherController.php @@ -29,6 +29,15 @@ class SubjectTeacherController extends Controller return redirect()->route('teachers.show', $this->service->create($request->validated(), $teacher)); } + public function show(Teacher $teacher, Subject $subject): View + { + return view('subject-teacher.show', [ + 'teacher' => $teacher, + 'subject' => $subject, + 'grades' => $this->service->getGrades($teacher, $subject), + ]); + } + public function edit(Teacher $teacher, Subject $subject): View { return view('subject-teacher.edit', [ diff --git a/app/Http/Requests/GradeTeacherPostRequest.php b/app/Http/Requests/GradeTeacherPostRequest.php new file mode 100644 index 0000000..86502e4 --- /dev/null +++ b/app/Http/Requests/GradeTeacherPostRequest.php @@ -0,0 +1,28 @@ +|string> + */ + public function rules(): array + { + return [ + 'grade_id' => 'required|exists:grades,id', + ]; + } +} diff --git a/app/Providers/ModelServiceProvider.php b/app/Providers/ModelServiceProvider.php index 72587d4..e7971b8 100644 --- a/app/Providers/ModelServiceProvider.php +++ b/app/Providers/ModelServiceProvider.php @@ -4,12 +4,14 @@ namespace App\Providers; use App\Http\Controllers\GradeController; use App\Http\Controllers\GradeSubjectController; +use App\Http\Controllers\GradeTeacherController; use App\Http\Controllers\StudentController; use App\Http\Controllers\SubjectController; use App\Http\Controllers\SubjectTeacherController; use App\Http\Controllers\TeacherController; use App\Services\GradeService; use App\Services\GradeSubjectService; +use App\Services\GradeTeacherService; use App\Services\ServiceInterface; use App\Services\StudentService; use App\Services\SubjectService; @@ -59,6 +61,12 @@ class ModelServiceProvider extends ServiceProvider ->give(function () { return new GradeSubjectService(); }); + + $this->app->when(GradeTeacherController::class) + ->needs(ServiceInterface::class) + ->give(function () { + return new GradeTeacherService(); + }); } /** diff --git a/app/Services/GradeTeacherService.php b/app/Services/GradeTeacherService.php new file mode 100644 index 0000000..4ed24bc --- /dev/null +++ b/app/Services/GradeTeacherService.php @@ -0,0 +1,47 @@ +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; + } +} diff --git a/app/Services/SubjectTeacherService.php b/app/Services/SubjectTeacherService.php index d5ea630..8e22055 100644 --- a/app/Services/SubjectTeacherService.php +++ b/app/Services/SubjectTeacherService.php @@ -19,6 +19,14 @@ class SubjectTeacherService implements ServiceInterface return Subject::all(); } + public function getGrades(Teacher $teacher, Subject $subject): Collection + { + return $teacher + ->grades() + ->join('grade_subject', 'grades.id', '=', 'grade_subject.grade_id') + ->where('subject_id', $subject->id)->get(); + } + public function create(array $data, ?Model $model = null): Teacher { $model->subjects()->syncWithoutDetaching($data['subject_id']); diff --git a/resources/views/grade-teacher/create.blade.php b/resources/views/grade-teacher/create.blade.php new file mode 100644 index 0000000..45e235c --- /dev/null +++ b/resources/views/grade-teacher/create.blade.php @@ -0,0 +1,5 @@ +@extends('layouts.app') + +@section('content') + @include('grade-teacher.form', ['route' => route('teachers.subjects.grades.store', [$teacher, $subject]), 'method' => 'POST']) +@endsection diff --git a/resources/views/grade-teacher/edit.blade.php b/resources/views/grade-teacher/edit.blade.php new file mode 100644 index 0000000..0a038f6 --- /dev/null +++ b/resources/views/grade-teacher/edit.blade.php @@ -0,0 +1,8 @@ +@extends('layouts.app') + +@section('content') + @include('grade-teacher.form', [ + 'route' => route('teachers.subjects.grades.update', + ['teacher' => $teacher, 'subject' => $subject, 'grade' => $updateGrade] + ), 'method' => 'PUT']) +@endsection diff --git a/resources/views/grade-teacher/form.blade.php b/resources/views/grade-teacher/form.blade.php new file mode 100644 index 0000000..7372494 --- /dev/null +++ b/resources/views/grade-teacher/form.blade.php @@ -0,0 +1,27 @@ +
+
+
+
+ {{ $subject->name }} +
+
+
+ @csrf + @method($method) +
+ + +
+ +
+
+
+
+
diff --git a/resources/views/subject-teacher/show.blade.php b/resources/views/subject-teacher/show.blade.php new file mode 100644 index 0000000..3438b82 --- /dev/null +++ b/resources/views/subject-teacher/show.blade.php @@ -0,0 +1,52 @@ +@extends('layouts.app') + +@section('content') +
+
+
+
+ {{ $subject->name }} +
+
+ @if(count($grades)) + + + + + + + @foreach ($grades as $grade) + + + + + + @endforeach + +
Название 
+
+ {{ $grade->name }} +
+
+ + +
+ @csrf + @method('DELETE') + +
+
+ @else +

У учителя отсутствуют классы

+ @endif +
+ +
+
+
+@endsection + diff --git a/resources/views/teachers/show.blade.php b/resources/views/teachers/show.blade.php index 8f7cc2e..0286934 100644 --- a/resources/views/teachers/show.blade.php +++ b/resources/views/teachers/show.blade.php @@ -35,7 +35,7 @@
- {{ $subject->name }} + {{ $subject->name }}
diff --git a/routes/web.php b/routes/web.php index 8d721f1..59933d2 100644 --- a/routes/web.php +++ b/routes/web.php @@ -2,6 +2,7 @@ use App\Http\Controllers\GradeController; use App\Http\Controllers\GradeSubjectController; +use App\Http\Controllers\GradeTeacherController; use App\Http\Controllers\StudentController; use App\Http\Controllers\SubjectController; use App\Http\Controllers\SubjectTeacherController; @@ -19,5 +20,6 @@ Route::resources([ 'teachers' => TeacherController::class, ]); -Route::resource('teachers.subjects', SubjectTeacherController::class)->except('index', 'show'); +Route::resource('teachers.subjects', SubjectTeacherController::class)->except('index'); +Route::resource('teachers.subjects.grades', GradeTeacherController::class)->except('index', 'show'); Route::resource('grades.subjects', GradeSubjectController::class)->except('index', 'show'); -- 2.25.1