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 @@ +
Классы отсутствуют
@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 @@Название | ++ + + @foreach ($subjects as $subject) + | |
---|---|---|
+
+ {{ $subject->name }}
+
+ |
+
+
+ Редактировать
+
+ |
+ + + | +
У класса отсутствуют предметы
+ @endif +Ученики остутствуют
@endif -Предметы отсутствуют
@endifУчителя отсутствуют
@endifУ учителя отсутствуют предметы
@endif