Added binding grade with teachers
This commit is contained in:
parent
2ccf102e5e
commit
2b7f8cf2df
62
app/Http/Controllers/GradeTeacherController.php
Normal file
62
app/Http/Controllers/GradeTeacherController.php
Normal 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,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
@ -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', [
|
||||||
|
28
app/Http/Requests/GradeTeacherPostRequest.php
Normal file
28
app/Http/Requests/GradeTeacherPostRequest.php
Normal 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',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
@ -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();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
47
app/Services/GradeTeacherService.php
Normal file
47
app/Services/GradeTeacherService.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
@ -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']);
|
||||||
|
5
resources/views/grade-teacher/create.blade.php
Normal file
5
resources/views/grade-teacher/create.blade.php
Normal 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
|
8
resources/views/grade-teacher/edit.blade.php
Normal file
8
resources/views/grade-teacher/edit.blade.php
Normal 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
|
27
resources/views/grade-teacher/form.blade.php
Normal file
27
resources/views/grade-teacher/form.blade.php
Normal 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>
|
52
resources/views/subject-teacher/show.blade.php
Normal file
52
resources/views/subject-teacher/show.blade.php
Normal 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> </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
|
||||||
|
|
@ -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>
|
||||||
|
@ -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');
|
||||||
|
Loading…
Reference in New Issue
Block a user