Added binding grade with teachers

This commit is contained in:
m.zargarov 2024-05-07 15:05:10 +04:00
parent 2ccf102e5e
commit 2b7f8cf2df
12 changed files with 258 additions and 2 deletions

View File

@ -0,0 +1,62 @@
<?php
namespace App\Http\Controllers;
use App\Http\Requests\GradeTeacherPostRequest;
use App\Models\Grade;
use App\Models\Subject;
use App\Models\Teacher;
use App\Services\ServiceInterface;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;
class GradeTeacherController extends Controller
{
public function __construct(
protected ServiceInterface $service,
) {
}
public function create(Teacher $teacher, Subject $subject): View
{
return view('grade-teacher.create', [
'teacher' => $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,
]);
}
}

View File

@ -29,6 +29,15 @@ class SubjectTeacherController extends Controller
return redirect()->route('teachers.show', $this->service->create($request->validated(), $teacher)); 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 public function edit(Teacher $teacher, Subject $subject): View
{ {
return view('subject-teacher.edit', [ return view('subject-teacher.edit', [

View File

@ -0,0 +1,28 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class GradeTeacherPostRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'grade_id' => 'required|exists:grades,id',
];
}
}

View File

@ -4,12 +4,14 @@ namespace App\Providers;
use App\Http\Controllers\GradeController; use App\Http\Controllers\GradeController;
use App\Http\Controllers\GradeSubjectController; use App\Http\Controllers\GradeSubjectController;
use App\Http\Controllers\GradeTeacherController;
use App\Http\Controllers\StudentController; use App\Http\Controllers\StudentController;
use App\Http\Controllers\SubjectController; use App\Http\Controllers\SubjectController;
use App\Http\Controllers\SubjectTeacherController; use App\Http\Controllers\SubjectTeacherController;
use App\Http\Controllers\TeacherController; use App\Http\Controllers\TeacherController;
use App\Services\GradeService; use App\Services\GradeService;
use App\Services\GradeSubjectService; use App\Services\GradeSubjectService;
use App\Services\GradeTeacherService;
use App\Services\ServiceInterface; use App\Services\ServiceInterface;
use App\Services\StudentService; use App\Services\StudentService;
use App\Services\SubjectService; use App\Services\SubjectService;
@ -59,6 +61,12 @@ class ModelServiceProvider extends ServiceProvider
->give(function () { ->give(function () {
return new GradeSubjectService(); return new GradeSubjectService();
}); });
$this->app->when(GradeTeacherController::class)
->needs(ServiceInterface::class)
->give(function () {
return new GradeTeacherService();
});
} }
/** /**

View File

@ -0,0 +1,47 @@
<?php
namespace App\Services;
use App\Models\Subject;
use App\Models\Teacher;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
class GradeTeacherService implements ServiceInterface
{
public function getAll(?Teacher $teacher = null, ?Subject $subject = null): Collection
{
return $teacher
->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;
}
}

View File

@ -19,6 +19,14 @@ class SubjectTeacherService implements ServiceInterface
return Subject::all(); 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 public function create(array $data, ?Model $model = null): Teacher
{ {
$model->subjects()->syncWithoutDetaching($data['subject_id']); $model->subjects()->syncWithoutDetaching($data['subject_id']);

View File

@ -0,0 +1,5 @@
@extends('layouts.app')
@section('content')
@include('grade-teacher.form', ['route' => route('teachers.subjects.grades.store', [$teacher, $subject]), 'method' => 'POST'])
@endsection

View File

@ -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

View File

@ -0,0 +1,27 @@
<div class="container col-md-6">
<div class="row justify-content-center">
<div class="card mt-4">
<div class="card-header">
{{ $subject->name }}
</div>
<div class="card-body">
<form action="{{ $route }}" method="POST">
@csrf
@method($method)
<div class="mb-3">
<label for="grade_id" class="form-label">Класс:</label>
<select id="grade_id" name="grade_id" class="form-select" required>
<option value="">Выберите класс</option>
@foreach($grades as $grade)
<option value="{{ $grade->id }}" {{ isset($updateGrade) && $updateGrade->id == $grade->id ? 'selected' : '' }}>
{{ $grade->name }}
</option>
@endforeach
</select>
</div>
<button type="submit" class="btn btn-success">Подтвердить</button>
</form>
</div>
</div>
</div>
</div>

View File

@ -0,0 +1,52 @@
@extends('layouts.app')
@section('content')
<div class="container col-md-6">
<div class="row justify-content-center">
<div class="card mt-4">
<div class="card-header">
{{ $subject->name }}
</div>
<div class="card-body">
@if(count($grades))
<table class="table table-striped">
<thead>
<th>Название</th>
<th>&nbsp;</th>
</thead>
<tbody>
@foreach ($grades as $grade)
<tr>
<td class="table-text">
<div>
{{ $grade->name }}
</div>
</td>
<td>
<div>
<a href="{{ route('teachers.subjects.grades.edit', [$teacher, $subject, $grade]) }}" class="btn btn-warning">Редактировать</a>
</div>
</td>
<td>
<form action="{{ route('teachers.subjects.grades.destroy', [$teacher, $subject, $grade]) }}" method="POST" style="display: inline-block;">
@csrf
@method('DELETE')
<button type="submit" class="btn btn-danger">Удалить</button>
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
@else
<p>У учителя отсутствуют классы</p>
@endif
</div>
<div class="card-footer">
<a href="{{ route('teachers.subjects.grades.create', [$teacher, $subject]) }}" class="btn btn-success">Добавить</a>
</div>
</div>
</div>
</div>
@endsection

View File

@ -35,7 +35,7 @@
<tr> <tr>
<td class="table-text"> <td class="table-text">
<div> <div>
{{ $subject->name }} <a href="{{ route('teachers.subjects.show', [$teacher, $subject]) }}" class="btn btn-block col-8">{{ $subject->name }}</a>
</div> </div>
</td> </td>
<td> <td>

View File

@ -2,6 +2,7 @@
use App\Http\Controllers\GradeController; use App\Http\Controllers\GradeController;
use App\Http\Controllers\GradeSubjectController; use App\Http\Controllers\GradeSubjectController;
use App\Http\Controllers\GradeTeacherController;
use App\Http\Controllers\StudentController; use App\Http\Controllers\StudentController;
use App\Http\Controllers\SubjectController; use App\Http\Controllers\SubjectController;
use App\Http\Controllers\SubjectTeacherController; use App\Http\Controllers\SubjectTeacherController;
@ -19,5 +20,6 @@ Route::resources([
'teachers' => TeacherController::class, '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'); Route::resource('grades.subjects', GradeSubjectController::class)->except('index', 'show');