task-4 (many-to-many) #4

Merged
klllst merged 5 commits from feature/task-4 into develop 2024-05-07 15:09:03 +04:00
45 changed files with 847 additions and 162 deletions

View File

@ -11,8 +11,9 @@ use Illuminate\View\View;
class GradeController extends Controller class GradeController extends Controller
{ {
public function __construct( public function __construct(
protected ServiceInterface $gradeService, protected ServiceInterface $service,
){} ) {
}
/** /**
* Display a listing of the resource. * Display a listing of the resource.
@ -20,7 +21,7 @@ class GradeController extends Controller
public function index(): View public function index(): View
{ {
return view('grades.index', [ return view('grades.index', [
'grades' => $this->gradeService->getAll(), 'grades' => $this->service->getAll(),
]); ]);
} }
@ -37,7 +38,7 @@ class GradeController extends Controller
*/ */
public function store(GradePostRequest $request): RedirectResponse public function store(GradePostRequest $request): RedirectResponse
{ {
return redirect()->route('grades.show', $this->gradeService->create($request->validated())); return redirect()->route('grades.show', $this->service->create($request->validated()));
} }
/** /**
@ -46,7 +47,8 @@ class GradeController extends Controller
public function show(Grade $grade): View public function show(Grade $grade): View
{ {
return view('grades.show', [ return view('grades.show', [
'grade' => $grade, 'grade' => $grade,
'subjects' => $grade->subjects,
]); ]);
} }
@ -65,7 +67,7 @@ class GradeController extends Controller
*/ */
public function update(GradePostRequest $request, Grade $grade): RedirectResponse public function update(GradePostRequest $request, Grade $grade): RedirectResponse
{ {
return redirect()->route('grades.show', $this->gradeService->update($grade, $request->validated())); return redirect()->route('grades.show', $this->service->update($grade, $request->validated()));
} }
/** /**
@ -73,7 +75,7 @@ class GradeController extends Controller
*/ */
public function destroy(Grade $grade): RedirectResponse public function destroy(Grade $grade): RedirectResponse
{ {
$this->gradeService->delete($grade); $this->service->delete($grade);
return redirect()->route('grades.index'); return redirect()->route('grades.index');
} }

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

@ -12,8 +12,9 @@ use Illuminate\View\View;
class StudentController extends Controller class StudentController extends Controller
{ {
public function __construct( public function __construct(
protected ServiceInterface $studentService protected ServiceInterface $service
){} ) {
}
/** /**
* Display a listing of the resource. * Display a listing of the resource.
@ -21,7 +22,7 @@ class StudentController extends Controller
public function index(): View public function index(): View
{ {
return view('students.index', [ return view('students.index', [
'students' => $this->studentService->getAll(), 'students' => $this->service->getAll(),
]); ]);
} }
@ -42,7 +43,7 @@ class StudentController extends Controller
{ {
return redirect()->route( return redirect()->route(
'students.show', 'students.show',
$this->studentService->create($request->validated()) $this->service->create($request->validated())
); );
} }
@ -75,7 +76,7 @@ class StudentController extends Controller
{ {
return redirect()->route( return redirect()->route(
'students.show', 'students.show',
$this->studentService->update($student, $request->validated()) $this->service->update($student, $request->validated())
); );
} }
@ -84,7 +85,7 @@ class StudentController extends Controller
*/ */
public function destroy(Student $student): RedirectResponse public function destroy(Student $student): RedirectResponse
{ {
$this->studentService->delete($student); $this->service->delete($student);
return redirect()->route('students.index'); return redirect()->route('students.index');
} }

View File

@ -11,8 +11,9 @@ use Illuminate\View\View;
class SubjectController extends Controller class SubjectController extends Controller
{ {
public function __construct( public function __construct(
protected ServiceInterface $subjectService, protected ServiceInterface $service,
){} ) {
}
/** /**
* Display a listing of the resource. * Display a listing of the resource.
@ -20,7 +21,7 @@ class SubjectController extends Controller
public function index(): View public function index(): View
{ {
return view('subjects.index', [ return view('subjects.index', [
"subjects" => $this->subjectService->getAll(), 'subjects' => $this->service->getAll(),
]); ]);
} }
@ -39,7 +40,7 @@ class SubjectController extends Controller
{ {
return redirect()->route( return redirect()->route(
'subjects.show', 'subjects.show',
$this->subjectService->create($request->validated()) $this->service->create($request->validated())
); );
} }
@ -49,7 +50,7 @@ class SubjectController extends Controller
public function show(Subject $subject): View public function show(Subject $subject): View
{ {
return view('subjects.show', [ return view('subjects.show', [
"subject" => $subject 'subject' => $subject,
]); ]);
} }
@ -70,7 +71,7 @@ class SubjectController extends Controller
{ {
return redirect()->route( return redirect()->route(
'subjects.show', 'subjects.show',
$this->subjectService->update($subject, $request->validated()) $this->service->update($subject, $request->validated())
); );
} }
@ -79,7 +80,7 @@ class SubjectController extends Controller
*/ */
public function destroy(Subject $subject): RedirectResponse public function destroy(Subject $subject): RedirectResponse
{ {
$this->subjectService->delete($subject); $this->service->delete($subject);
return redirect()->route('subjects.index'); return redirect()->route('subjects.index');
} }

View File

@ -0,0 +1,60 @@
<?php
namespace App\Http\Controllers;
use App\Http\Requests\SubjectTeacherPostRequest;
use App\Models\Subject;
use App\Models\Teacher;
use App\Services\ServiceInterface;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;
class SubjectTeacherController extends Controller
{
public function __construct(
protected ServiceInterface $service,
) {
}
public function create(Teacher $teacher): View
{
return view('subject-teacher.create', [
'teacher' => $teacher,
'subjects' => $this->service->getAllSubjects(),
]);
}
public function store(SubjectTeacherPostRequest $request, Teacher $teacher): RedirectResponse
{
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
{
return view('subject-teacher.edit', [
'teacher' => $teacher,
'updateSubject' => $subject,
'subjects' => $this->service->getAllSubjects(),
]);
}
public function update(SubjectTeacherPostRequest $request, Teacher $teacher, Subject $subject): RedirectResponse
{
return redirect()->route('teachers.show',
$this->service->update($teacher, $request->validated(), $subject));
}
public function destroy(Teacher $teacher, Subject $subject): RedirectResponse
{
return redirect()->route('teachers.show', $this->service->delete($teacher, $subject));
}
}

View File

@ -11,8 +11,9 @@ use Illuminate\View\View;
class TeacherController extends Controller class TeacherController extends Controller
{ {
public function __construct( public function __construct(
protected ServiceInterface $teacherService protected ServiceInterface $service
){} ) {
}
/** /**
* Display a listing of the resource. * Display a listing of the resource.
@ -20,7 +21,7 @@ class TeacherController extends Controller
public function index(): View public function index(): View
{ {
return view('teachers.index', [ return view('teachers.index', [
'teachers' => $this->teacherService->getAll(), 'teachers' => $this->service->getAll(),
]); ]);
} }
@ -39,7 +40,7 @@ class TeacherController extends Controller
{ {
return redirect()->route( return redirect()->route(
'teachers.show', 'teachers.show',
$this->teacherService->create($request->validated()) $this->service->create($request->validated())
); );
} }
@ -50,6 +51,7 @@ class TeacherController extends Controller
{ {
return view('teachers.show', [ return view('teachers.show', [
'teacher' => $teacher, 'teacher' => $teacher,
'subjects' => $teacher->subjects,
]); ]);
} }
@ -70,7 +72,7 @@ class TeacherController extends Controller
{ {
return redirect()->route( return redirect()->route(
'teachers.show', 'teachers.show',
$this->teacherService->update($teacher, $request->validated()) $this->service->update($teacher, $request->validated())
); );
} }
@ -79,7 +81,7 @@ class TeacherController extends Controller
*/ */
public function destroy(Teacher $teacher): RedirectResponse public function destroy(Teacher $teacher): RedirectResponse
{ {
$this->teacherService->delete($teacher); $this->service->delete($teacher);
return redirect()->route('teachers.index'); return redirect()->route('teachers.index');
} }

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

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

@ -2,8 +2,6 @@
namespace App\Http\Requests; namespace App\Http\Requests;
use App\Enums\RoleEnum;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Foundation\Http\FormRequest; use Illuminate\Foundation\Http\FormRequest;
class StudentPostRequest extends FormRequest class StudentPostRequest extends FormRequest

View File

@ -22,7 +22,7 @@ class SubjectPostRequest extends FormRequest
public function rules(): array public function rules(): array
{ {
return [ return [
'name' => 'required|unique:grades|max:255', 'name' => 'required|unique:subjects|max:255',
]; ];
} }
} }

View File

@ -0,0 +1,28 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class SubjectTeacherPostRequest 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

@ -2,8 +2,6 @@
namespace App\Http\Requests; namespace App\Http\Requests;
use App\Enums\RoleEnum;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Foundation\Http\FormRequest; use Illuminate\Foundation\Http\FormRequest;
class TeacherPostRequest extends FormRequest class TeacherPostRequest extends FormRequest

View File

@ -2,12 +2,13 @@
namespace App\Models; namespace App\Models;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphOne; use Illuminate\Database\Eloquent\Relations\MorphOne;
use Illuminate\Database\Eloquent\Builder;
class Student extends Model class Student extends Model
{ {
use HasFactory; use HasFactory;
@ -33,7 +34,7 @@ class Student extends Model
public function scopeFilter(Builder $query): void public function scopeFilter(Builder $query): void
{ {
$name = request('name'); $name = request('name');
$query->when($name, function (Builder $query, $name){ $query->when($name, function (Builder $query, $name) {
$query->whereRaw('CONCAT (name, \' \', surname, \' \', patronymic) ilike ?', ["$name%"]); $query->whereRaw('CONCAT (name, \' \', surname, \' \', patronymic) ilike ?', ["$name%"]);
$query->orWhereRaw('CONCAT (name, \' \', patronymic, \' \', surname) ilike ?', ["$name%"]); $query->orWhereRaw('CONCAT (name, \' \', patronymic, \' \', surname) ilike ?', ["$name%"]);
$query->orWhereRaw('CONCAT (surname, \' \', name, \' \', patronymic) ilike ?', ["$name%"]); $query->orWhereRaw('CONCAT (surname, \' \', name, \' \', patronymic) ilike ?', ["$name%"]);
@ -46,7 +47,7 @@ class Student extends Model
public function fio(): Attribute public function fio(): Attribute
{ {
return Attribute::make( return Attribute::make(
get: fn () => $this->last_name . " " . $this->name . " " . $this->middle_name, get: fn () => $this->last_name.' '.$this->name.' '.$this->middle_name,
); );
} }
} }

View File

@ -2,12 +2,13 @@
namespace App\Models; namespace App\Models;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\MorphOne; use Illuminate\Database\Eloquent\Relations\MorphOne;
use Illuminate\Database\Eloquent\Builder;
class Teacher extends Model class Teacher extends Model
{ {
use HasFactory; use HasFactory;
@ -37,7 +38,7 @@ class Teacher extends Model
public function scopeFilter(Builder $query): void public function scopeFilter(Builder $query): void
{ {
$name = request('name'); $name = request('name');
$query->when($name, function (Builder $query, $name){ $query->when($name, function (Builder $query, $name) {
$query->whereRaw('CONCAT (name, \' \', surname, \' \', patronymic) ilike ?', ["$name%"]); $query->whereRaw('CONCAT (name, \' \', surname, \' \', patronymic) ilike ?', ["$name%"]);
$query->orWhereRaw('CONCAT (name, \' \', patronymic, \' \', surname) ilike ?', ["$name%"]); $query->orWhereRaw('CONCAT (name, \' \', patronymic, \' \', surname) ilike ?', ["$name%"]);
$query->orWhereRaw('CONCAT (surname, \' \', name, \' \', patronymic) ilike ?', ["$name%"]); $query->orWhereRaw('CONCAT (surname, \' \', name, \' \', patronymic) ilike ?', ["$name%"]);
@ -50,7 +51,7 @@ class Teacher extends Model
public function fio(): Attribute public function fio(): Attribute
{ {
return Attribute::make( return Attribute::make(
get: fn () => $this->last_name . " " . $this->name . " " . $this->middle_name, get: fn () => $this->last_name.' '.$this->name.' '.$this->middle_name,
); );
} }
} }

View File

@ -3,13 +3,19 @@
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\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\TeacherController; use App\Http\Controllers\TeacherController;
use App\Services\GradeService; use App\Services\GradeService;
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;
use App\Services\SubjectTeacherService;
use App\Services\TeacherService; use App\Services\TeacherService;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
@ -43,6 +49,24 @@ class ModelServiceProvider extends ServiceProvider
->give(function () { ->give(function () {
return new SubjectService(); return new SubjectService();
}); });
$this->app->when(SubjectTeacherController::class)
->needs(ServiceInterface::class)
->give(function () {
return new SubjectTeacherService();
});
$this->app->when(GradeSubjectController::class)
->needs(ServiceInterface::class)
->give(function () {
return new GradeSubjectService();
});
$this->app->when(GradeTeacherController::class)
->needs(ServiceInterface::class)
->give(function () {
return new GradeTeacherService();
});
} }
/** /**

View File

@ -3,14 +3,14 @@
namespace App\Services; namespace App\Services;
use App\Models\Grade; use App\Models\Grade;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Pagination\LengthAwarePaginator;
class GradeService implements ServiceInterface class GradeService implements ServiceInterface
{ {
public function getAll(): LengthAwarePaginator public function getAll(): LengthAwarePaginator
{ {
return Grade::filter()->paginate(10)->withQueryString(); return Grade::filter()->paginate(5)->withQueryString();
} }
public function create(array $data): Grade public function create(array $data): Grade
@ -18,7 +18,7 @@ class GradeService implements ServiceInterface
return Grade::create($data); return Grade::create($data);
} }
public function update($model, array $data): Grade public function update(Model $model, array $data): Grade
{ {
$model->update($data); $model->update($data);

View File

@ -0,0 +1,43 @@
<?php
namespace App\Services;
use App\Models\Grade;
use App\Models\Subject;
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,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

@ -2,13 +2,15 @@
namespace App\Services; namespace App\Services;
use Illuminate\Database\Eloquent\Model;
interface ServiceInterface interface ServiceInterface
{ {
public function getAll(); public function getAll();
public function create(array $data); public function create(array $data): Model;
public function update($model, array $data); public function update(Model $model, array $data): Model;
public function delete($model); public function delete(Model $model);
} }

View File

@ -3,7 +3,6 @@
namespace App\Services; namespace App\Services;
use App\Models\Student; use App\Models\Student;
use App\Models\Teacher;
use App\Models\User; use App\Models\User;
use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Pagination\LengthAwarePaginator;
@ -11,7 +10,7 @@ class StudentService implements ServiceInterface
{ {
public function getAll(): LengthAwarePaginator public function getAll(): LengthAwarePaginator
{ {
return Student::filter()->paginate(10)->withQueryString(); return Student::filter()->paginate(5)->withQueryString();
} }
public function create(array $data): Student public function create(array $data): Student

View File

@ -3,14 +3,14 @@
namespace App\Services; namespace App\Services;
use App\Models\Subject; use App\Models\Subject;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Pagination\LengthAwarePaginator;
class SubjectService implements ServiceInterface class SubjectService implements ServiceInterface
{ {
public function getAll(): LengthAwarePaginator public function getAll(): LengthAwarePaginator
{ {
return Subject::filter()->paginate(10)->withQueryString(); return Subject::filter()->paginate(5)->withQueryString();
} }
public function create(array $data): Subject public function create(array $data): Subject
@ -18,14 +18,14 @@ class SubjectService implements ServiceInterface
return Subject::create($data); return Subject::create($data);
} }
public function update($model, array $data): Subject public function update(Model $model, array $data): Subject
{ {
$model->update($data); $model->update($data);
return $model; return $model;
} }
public function delete($model): void public function delete(Model $model): void
{ {
$model->delete(); $model->delete();
} }

View File

@ -0,0 +1,51 @@
<?php
namespace App\Services;
use App\Models\Subject;
use App\Models\Teacher;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
class SubjectTeacherService implements ServiceInterface
{
public function getAll(?Teacher $teacher = null): Collection
{
return $teacher->subjects;
}
public function getAllSubjects(): Collection
{
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
{
$model->subjects()->syncWithoutDetaching($data['subject_id']);
return $model;
}
public function update(Model $model, array $data, ?Model $subject = null): Teacher
{
$model->subjects()->detach($subject->id);
$model->subjects()->attach($data['subject_id']);
return $model;
}
public function delete(Model $model, ?Model $subject = null): Teacher
{
$model->subjects()->detach($subject);
return $model;
}
}

View File

@ -2,7 +2,6 @@
namespace App\Services; namespace App\Services;
use App\Models\Student;
use App\Models\Teacher; use App\Models\Teacher;
use App\Models\User; use App\Models\User;
use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Pagination\LengthAwarePaginator;
@ -11,7 +10,7 @@ class TeacherService implements ServiceInterface
{ {
public function getAll(): LengthAwarePaginator public function getAll(): LengthAwarePaginator
{ {
return Teacher::filter()->paginate(2)->withQueryString(); return Teacher::filter()->paginate(5)->withQueryString();
} }
public function create(array $data): Teacher public function create(array $data): Teacher

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

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

@ -3,12 +3,6 @@
@section('content') @section('content')
<div class="container col-md-6"> <div class="container col-md-6">
<div class="row justify-content-center"> <div class="row justify-content-center">
<div class="card">
<div class="card-header">{{__('Новый класс')}}</div>
<div class="card-body">
<a href="{{ route('grades.create') }}" class="btn btn-success">Добавить</a>
</div>
</div>
<div class="card mt-4"> <div class="card mt-4">
<div class="card-header">{{__('Поиск класса')}}</div> <div class="card-header">{{__('Поиск класса')}}</div>
<div class="card-body"> <div class="card-body">
@ -24,10 +18,10 @@
</form> </form>
</div> </div>
</div> </div>
@if (count($grades))
<div class="card mt-4"> <div class="card mt-4">
<div class="card-header">{{__('Классы')}}</div> <div class="card-header">{{__('Классы')}}</div>
<div class="card-body"> <div class="card-body">
@if (count($grades))
<table class="table table-striped"> <table class="table table-striped">
<thead> <thead>
<th>Название</th> <th>Название</th>
@ -64,10 +58,15 @@
@endforeach @endforeach
</tbody> </tbody>
</table> </table>
@else
<p>Классы отсутствуют</p>
@endif
</div>
<div class="card-footer">
<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>
</div> </div>
@endif
</div> </div>
</div> </div>
@endsection @endsection

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

@ -1,5 +1,5 @@
@extends('layouts.app') @extends('layouts.app')
@section('content') @section('content')
@include('students.form', ['route' => route('students.update', $user), 'method' => 'PUT']) @include('students.form', ['route' => route('students.update', $student), 'method' => 'PUT'])
@endsection @endsection

View File

@ -34,11 +34,11 @@
</div> </div>
<div class="mb-3"> <div class="mb-3">
<label for="email" class="form-label">Эл. почта:</label> <label for="email" class="form-label">Эл. почта:</label>
<input type="text" id="email" name="email" class="form-control" value="{{ isset($student) ? $student->email : old('email') }}" required> <input type="text" id="email" name="email" class="form-control" value="{{ isset($student) ? $student->user->email : old('email') }}" required>
</div> </div>
<div class="mb-3"> <div class="mb-3">
<label for="password" class="form-label">Пароль:</label> <label for="password" class="form-label">Пароль:</label>
<input type="text" id="password" name="password" class="form-control" value="{{ isset($student) ? $student->password : old('password') }}" required> <input type="text" id="password" name="password" class="form-control" value="{{ isset($student) ? $student->user->password : old('password') }}" required>
</div> </div>
<button type="submit" class="btn btn-success">Подтвердить</button> <button type="submit" class="btn btn-success">Подтвердить</button>
</form> </form>

View File

@ -3,12 +3,6 @@
@section('content') @section('content')
<div class="container col-md-6"> <div class="container col-md-6">
<div class="row justify-content-center"> <div class="row justify-content-center">
<div class="card">
<div class="card-header">{{__('Новый ученик')}}</div>
<div class="card-body">
<a href="{{ route('students.create') }}" class="btn btn-success">Добавить</a>
</div>
</div>
<div class="card mt-4"> <div class="card mt-4">
<div class="card-header">{{__('Поиск ученика')}}</div> <div class="card-header">{{__('Поиск ученика')}}</div>
<div class="card-body"> <div class="card-body">
@ -23,43 +17,48 @@
</form> </form>
</div> </div>
</div> </div>
@if (count($students)) <div class="card mt-4">
<div class="card mt-4"> <div class="card-header">{{__('Ученики')}}</div>
<div class="card-header">{{__('Ученики')}}</div> <div class="card-body">
<div class="card-body"> @if(count($students))
<table class="table table-striped"> <table class="table table-striped">
<thead> <thead>
<th>ФИО</th> <th>ФИО</th>
<th>&nbsp;</th> <th>&nbsp;</th>
</thead> </thead>
<tbody> <tbody>
@foreach ($students as $student) @foreach ($students as $student)
<tr> <tr>
<td class="table-text"> <td class="table-text">
<div> <div>
<a href="{{ route('students.show', $student) }}" class="btn btn-block col-8">{{ $student->last_name }} {{ $student->name }} {{ $student->middle_name }}</a> <a href="{{ route('students.show', $student) }}" class="btn btn-block col-8">{{ $student->last_name }} {{ $student->name }} {{ $student->middle_name }}</a>
</div> </div>
</td> </td>
<td> <td>
<div> <div>
<a href="{{ route('students.edit', $student) }}" class="btn btn-warning">Редактировать</a> <a href="{{ route('students.edit', $student) }}" class="btn btn-warning">Редактировать</a>
</div> </div>
</td> </td>
<td> <td>
<form action="{{ route('students.destroy', $student) }}" method="POST" style="display: inline-block;"> <form action="{{ route('students.destroy', $student) }}" method="POST" style="display: inline-block;">
@csrf @csrf
@method('DELETE') @method('DELETE')
<button type="submit" class="btn btn-danger">Удалить</button> <button type="submit" class="btn btn-danger">Удалить</button>
</form> </form>
</td> </td>
</tr> </tr>
@endforeach @endforeach
</tbody> </tbody>
</table> </table>
</div> @else
<div class="card-footer">{{ $students->links() }}</div> <p>Ученики остутствуют</p>
@endif
</div> </div>
@endif <div class="card-footer">
<a href="{{ route('students.create') }}" class="btn btn-success">Добавить</a>
</div>
<div class="card-footer">{{ $students->links() }}</div>
</div>
</div> </div>
</div> </div>
@endsection @endsection

View File

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

View File

@ -0,0 +1,8 @@
@extends('layouts.app')
@section('content')
@include('subject-teacher.form', [
'route' => route('teachers.subjects.update',
['teacher' => $teacher, '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

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

@ -3,12 +3,6 @@
@section('content') @section('content')
<div class="container col-md-6"> <div class="container col-md-6">
<div class="row justify-content-center"> <div class="row justify-content-center">
<div class="card">
<div class="card-header">{{__('Новый предмет')}}</div>
<div class="card-body">
<a href="{{ route('subjects.create') }}" class="btn btn-success">Добавить</a>
</div>
</div>
<div class="card mt-4"> <div class="card mt-4">
<div class="card-header">{{__('Поиск предмета')}}</div> <div class="card-header">{{__('Поиск предмета')}}</div>
<div class="card-body"> <div class="card-body">
@ -23,43 +17,48 @@
</form> </form>
</div> </div>
</div> </div>
@if (count($subjects)) <div class="card mt-4">
<div class="card mt-4"> <div class="card-header">{{__('Предметы')}}</div>
<div class="card-header">{{__('Предметы')}}</div> <div class="card-body">
<div class="card-body"> @if (count($subjects))
<table class="table table-striped"> <table class="table table-striped">
<thead> <thead>
<th>Название</th> <th>Название</th>
<th>&nbsp;</th> <th>&nbsp;</th>
</thead> </thead>
<tbody> <tbody>
@foreach ($subjects as $subject) @foreach ($subjects as $subject)
<tr> <tr>
<td class="table-text"> <td class="table-text">
<div> <div>
<a href="{{ route('subjects.show', $subject) }}" class="btn btn-block col-8">{{ $subject->name }}</a> <a href="{{ route('subjects.show', $subject) }}" class="btn btn-block col-8">{{ $subject->name }}</a>
</div> </div>
</td> </td>
<td> <td>
<div> <div>
<a href="{{ route('subjects.edit', $subject) }}" class="btn btn-warning">Редактировать</a> <a href="{{ route('subjects.edit', $subject) }}" class="btn btn-warning">Редактировать</a>
</div> </div>
</td> </td>
<td> <td>
<form action="{{ route('subjects.destroy', $subject) }}" method="POST" style="display: inline-block;"> <form action="{{ route('subjects.destroy', $subject) }}" method="POST" style="display: inline-block;">
@csrf @csrf
@method('DELETE') @method('DELETE')
<button type="submit" class="btn btn-danger">Удалить</button> <button type="submit" class="btn btn-danger">Удалить</button>
</form> </form>
</td> </td>
</tr> </tr>
@endforeach @endforeach
</tbody> </tbody>
</table> </table>
</div> @else
<div class="card-footer">{{ $subjects->links() }}</div> <p>Предметы отсутствуют</p>
@endif
</div> </div>
@endif <div class="card-footer">
<a href="{{ route('subjects.create') }}" class="btn btn-success">Добавить</a>
</div>
<div class="card-footer">{{ $subjects->links() }}</div>
</div>
</div> </div>
</div> </div>
@endsection @endsection

View File

@ -1,5 +1,5 @@
@extends('layouts.app') @extends('layouts.app')
@section('content') @section('content')
@include('teachers.form', ['route' => route('teachers.update', $user), 'method' => 'PUT']) @include('teachers.form', ['route' => route('teachers.update', $teacher), 'method' => 'PUT'])
@endsection @endsection

View File

@ -23,11 +23,11 @@
</div> </div>
<div class="mb-3"> <div class="mb-3">
<label for="email" class="form-label">Эл. почта:</label> <label for="email" class="form-label">Эл. почта:</label>
<input type="text" id="email" name="email" class="form-control" value="{{ isset($teacher) ? $teacher->email : old('email') }}" required> <input type="text" id="email" name="email" class="form-control" value="{{ isset($teacher) ? $teacher->user->email : old('email') }}" required>
</div> </div>
<div class="mb-3"> <div class="mb-3">
<label for="password" class="form-label">Пароль:</label> <label for="password" class="form-label">Пароль:</label>
<input type="text" id="password" name="password" class="form-control" value="{{ isset($teacher) ? $teacher->password : old('password') }}" required> <input type="text" id="password" name="password" class="form-control" value="{{ isset($teacher) ? $teacher->user->password : old('password') }}" required>
</div> </div>
<button type="submit" class="btn btn-success">Подтвердить</button> <button type="submit" class="btn btn-success">Подтвердить</button>
</form> </form>

View File

@ -3,12 +3,6 @@
@section('content') @section('content')
<div class="container col-md-6"> <div class="container col-md-6">
<div class="row justify-content-center"> <div class="row justify-content-center">
<div class="card">
<div class="card-header">{{__('Новый учитель')}}</div>
<div class="card-body">
<a href="{{ route('teachers.create') }}" class="btn btn-success">Добавить</a>
</div>
</div>
<div class="card mt-4"> <div class="card mt-4">
<div class="card-header">{{__('Поиск учителя')}}</div> <div class="card-header">{{__('Поиск учителя')}}</div>
<div class="card-body"> <div class="card-body">
@ -23,10 +17,10 @@
</form> </form>
</div> </div>
</div> </div>
@if (count($teachers))
<div class="card mt-4"> <div class="card mt-4">
<div class="card-header">{{__('Учителя')}}</div> <div class="card-header">{{__('Учителя')}}</div>
<div class="card-body"> <div class="card-body">
@if (count($teachers))
<table class="table table-striped"> <table class="table table-striped">
<thead> <thead>
<th>ФИО</th> <th>ФИО</th>
@ -56,10 +50,15 @@
@endforeach @endforeach
</tbody> </tbody>
</table> </table>
@else
<p>Учителя отсутствуют</p>
@endif
</div>
<div class="card-footer">
<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>
</div> </div>
@endif
</div> </div>
</div> </div>
@endsection @endsection

View File

@ -4,7 +4,9 @@
<div class="container col-md-6"> <div class="container col-md-6">
<div class="row justify-content-center"> <div class="row justify-content-center">
<div class="card"> <div class="card">
<div class="card-header">{{ ('Учитель') }}</div> <div class="card-header">
{{ ('Учитель') }}
</div>
<div class="card-body"> <div class="card-body">
<div class="mb-3"> <div class="mb-3">
<h5><strong>ФИО: </strong>{{ $teacher->fio }}</h5> <h5><strong>ФИО: </strong>{{ $teacher->fio }}</h5>
@ -17,6 +19,49 @@
<a href="{{ route('teachers.edit', $teacher) }}" class="btn btn-primary">Редактировать</a> <a href="{{ route('teachers.edit', $teacher) }}" 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>
<a href="{{ route('teachers.subjects.show', [$teacher, $subject]) }}" class="btn btn-block col-8">{{ $subject->name }}</a>
</div>
</td>
<td>
<div>
<a href="{{ route('teachers.subjects.edit', [$teacher, $subject]) }}" class="btn btn-warning">Редактировать</a>
</div>
</td>
<td>
<form action="{{ route('teachers.subjects.destroy', [$teacher, $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('teachers.subjects.create', $teacher) }}" class="btn btn-success">Добавить</a>
</div>
</div>
</div> </div>
</div> </div>
@endsection @endsection

View File

@ -1,8 +1,11 @@
<?php <?php
use App\Http\Controllers\GradeController; use App\Http\Controllers\GradeController;
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\TeacherController; use App\Http\Controllers\TeacherController;
use Illuminate\Support\Facades\Route; use Illuminate\Support\Facades\Route;
@ -10,7 +13,13 @@ Route::get('/', function () {
return redirect()->route('grades.index'); return redirect()->route('grades.index');
}); });
Route::resource('grades', GradeController::class); Route::resources([
Route::resource('subjects', SubjectController::class); 'grades' => GradeController::class,
Route::resource('students', StudentController::class); 'subjects' => SubjectController::class,
Route::resource('teachers', TeacherController::class); 'students' => StudentController::class,
'teachers' => TeacherController::class,
]);
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');