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');