CourseWork/app/Http/Controllers/SubjectTeacherController.php
2024-06-22 23:59:16 +04:00

64 lines
1.8 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Http\Requests\SubjectTeacherPostRequest;
use App\Models\Subject;
use App\Models\Teacher;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;
class SubjectTeacherController extends Controller
{
public function create(Teacher $teacher): View
{
return view('subject-teacher.create', [
'teacher' => $teacher,
'subjects' => Subject::all(),
]);
}
public function store(SubjectTeacherPostRequest $request, Teacher $teacher): RedirectResponse
{
$teacher->subjects()->syncWithoutDetaching($request->subject_id);
return redirect()->route('teachers.show', $teacher);
}
public function show(Teacher $teacher, Subject $subject): View
{
return view('subject-teacher.show', [
'teacher' => $teacher,
'subject' => $subject,
'grades' => $teacher
->grades()
->join('grade_subject', 'grades.id', '=', 'grade_subject.grade_id')
->where('subject_id', $subject->id)->get(),
]);
}
public function edit(Teacher $teacher, Subject $subject): View
{
return view('subject-teacher.edit', [
'teacher' => $teacher,
'updateSubject' => $subject,
'subjects' => Subject::all(),
]);
}
public function update(SubjectTeacherPostRequest $request, Teacher $teacher, Subject $subject): RedirectResponse
{
$teacher->subjects()->detach($subject);
$teacher->subjects()->attach($request->subject_id);
return redirect()->route('teachers.show', $teacher);
}
public function destroy(Teacher $teacher, Subject $subject): RedirectResponse
{
$teacher->subjects()->detach($subject);
return redirect()->route('teachers.show', $teacher);
}
}