Added binding grade with subjects

This commit is contained in:
m.zargarov 2024-05-07 12:14:18 +04:00
parent fb2fda38bb
commit 622b359e1f
15 changed files with 219 additions and 5 deletions

View File

@ -48,6 +48,7 @@ class GradeController extends Controller
{ {
return view('grades.show', [ return view('grades.show', [
'grade' => $grade, 'grade' => $grade,
'subjects' => $grade->subjects,
]); ]);
} }

View File

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

View File

@ -0,0 +1,28 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class GradeSubjectPostRequest 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 [
'subject_id' => 'required|exists:subjects,id',
];
}
}

View File

@ -3,11 +3,13 @@
namespace App\Providers; namespace App\Providers;
use App\Http\Controllers\GradeController; use App\Http\Controllers\GradeController;
use App\Http\Controllers\GradeSubjectController;
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\ServiceInterface; use App\Services\ServiceInterface;
use App\Services\StudentService; use App\Services\StudentService;
use App\Services\SubjectService; use App\Services\SubjectService;
@ -51,6 +53,12 @@ class ModelServiceProvider extends ServiceProvider
->give(function () { ->give(function () {
return new SubjectTeacherService(); return new SubjectTeacherService();
}); });
$this->app->when(GradeSubjectController::class)
->needs(ServiceInterface::class)
->give(function () {
return new GradeSubjectService();
});
} }
/** /**

View File

@ -0,0 +1,44 @@
<?php
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;
class GradeSubjectService implements ServiceInterface
{
public function getAll(?Grade $grade = null): Collection
{
return $grade->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;
}
}

View File

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

View File

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

View File

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

View File

@ -62,7 +62,7 @@
<p>Классы отсутствуют</p> <p>Классы отсутствуют</p>
@endif @endif
</div> </div>
<div class="card-body"> <div class="card-footer">
<a href="{{ route('grades.create') }}" class="btn btn-success">Добавить</a> <a href="{{ route('grades.create') }}" class="btn btn-success">Добавить</a>
</div> </div>
<div class="card-footer">{{ $grades->links() }}</div> <div class="card-footer">{{ $grades->links() }}</div>

View File

@ -14,6 +14,49 @@
<a href="{{ route('grades.edit', $grade) }}" class="btn btn-primary">Редактировать</a> <a href="{{ route('grades.edit', $grade) }}" class="btn btn-primary">Редактировать</a>
</div> </div>
</div> </div>
<div class="card mt-4">
<div class="card-header">
{{__('Журнал предметов')}}
</div>
<div class="card-body">
@if(count($subjects))
<table class="table table-striped">
<thead>
<th>Название</th>
<th>&nbsp;</th>
</thead>
<tbody>
@foreach ($subjects as $subject)
<tr>
<td class="table-text">
<div>
{{ $subject->name }}
</div>
</td>
<td>
<div>
<a href="{{ route('grades.subjects.edit', [$grade, $subject]) }}" class="btn btn-warning">Редактировать</a>
</div>
</td>
<td>
<form action="{{ route('grades.subjects.destroy', [$grade, $subject]) }}" 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('grades.subjects.create', $grade) }}" class="btn btn-success">Добавить</a>
</div>
</div>
</div> </div>
</div> </div>
@endsection @endsection

View File

@ -54,7 +54,7 @@
<p>Ученики остутствуют</p> <p>Ученики остутствуют</p>
@endif @endif
</div> </div>
<div class="card-body"> <div class="card-footer">
<a href="{{ route('students.create') }}" class="btn btn-success">Добавить</a> <a href="{{ route('students.create') }}" class="btn btn-success">Добавить</a>
</div> </div>
<div class="card-footer">{{ $students->links() }}</div> <div class="card-footer">{{ $students->links() }}</div>

View File

@ -54,7 +54,7 @@
<p>Предметы отсутствуют</p> <p>Предметы отсутствуют</p>
@endif @endif
</div> </div>
<div class="card-body"> <div class="card-footer">
<a href="{{ route('subjects.create') }}" class="btn btn-success">Добавить</a> <a href="{{ route('subjects.create') }}" class="btn btn-success">Добавить</a>
</div> </div>
<div class="card-footer">{{ $subjects->links() }}</div> <div class="card-footer">{{ $subjects->links() }}</div>

View File

@ -54,7 +54,7 @@
<p>Учителя отсутствуют</p> <p>Учителя отсутствуют</p>
@endif @endif
</div> </div>
<div class="card-body"> <div class="card-footer">
<a href="{{ route('teachers.create') }}" class="btn btn-success">Добавить</a> <a href="{{ route('teachers.create') }}" class="btn btn-success">Добавить</a>
</div> </div>
<div class="card-footer">{{ $teachers->links() }}</div> <div class="card-footer">{{ $teachers->links() }}</div>

View File

@ -58,7 +58,7 @@
<p>У учителя отсутствуют предметы</p> <p>У учителя отсутствуют предметы</p>
@endif @endif
</div> </div>
<div class="card-body"> <div class="card-footer">
<a href="{{ route('teachers.subjects.create', $teacher) }}" class="btn btn-success">Добавить</a> <a href="{{ route('teachers.subjects.create', $teacher) }}" class="btn btn-success">Добавить</a>
</div> </div>
</div> </div>

View File

@ -1,6 +1,7 @@
<?php <?php
use App\Http\Controllers\GradeController; use App\Http\Controllers\GradeController;
use App\Http\Controllers\GradeSubjectController;
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,3 +20,4 @@ Route::resources([
]); ]);
Route::resource('teachers.subjects', SubjectTeacherController::class)->except('index', 'show'); Route::resource('teachers.subjects', SubjectTeacherController::class)->except('index', 'show');
Route::resource('grades.subjects', GradeSubjectController::class)->except('index', 'show');