applicant-site/app/Http/Controllers/admin/Catalog/FacultyController.php

78 lines
2.7 KiB
PHP
Raw Normal View History

2024-02-07 16:30:49 +03:00
<?php
2024-02-12 12:15:46 +03:00
namespace App\Http\Controllers\admin\Catalog;
2024-02-07 16:30:49 +03:00
use App\Http\Controllers\Controller;
2024-02-12 12:15:46 +03:00
use App\Http\Requests\admin\Catalog\StoreFacultyRequest;
use App\Http\Requests\admin\Catalog\UpdateFacultyRequest;
2024-02-07 16:30:49 +03:00
use App\Models\EducationalInstitution;
use App\Models\Faculty;
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
use Illuminate\Foundation\Application;
use Illuminate\Http\RedirectResponse;
class FacultyController extends Controller
{
public function index(): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
{
$faculties = Faculty::all();
2024-02-12 12:15:46 +03:00
return view('admin.catalog.faculty.index', compact('faculties'));
2024-02-07 16:30:49 +03:00
}
public function create(): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
{
$educationalInstitutions = EducationalInstitution::pluck('name', 'id');
2024-02-12 12:15:46 +03:00
return view('admin.catalog.faculty.create', compact('educationalInstitutions'));
2024-02-07 16:30:49 +03:00
}
public function store(StoreFacultyRequest $request): RedirectResponse
{
$validated = $request->validated();
$faculty = new Faculty();
$faculty->name = $validated['name'];
$faculty->description = $validated['description'];
$faculty->position = $validated['position'];
2024-02-12 12:15:46 +03:00
$faculty->slug = $validated['slug'];
2024-02-07 16:30:49 +03:00
$faculty->educational_institution_id = $validated['educational_institution_id'];
$faculty->save();
return redirect()->route('faculties.index');
}
public function show(Faculty $faculty): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
{
2024-02-12 12:15:46 +03:00
return view('admin.catalog.faculty.show', compact('faculty'));
}
2024-02-07 19:27:25 +03:00
public function edit(Faculty $faculty): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
2024-02-07 16:30:49 +03:00
{
$educationalInstitutions = EducationalInstitution::pluck('name', 'id');
2024-02-12 12:15:46 +03:00
return view('admin.catalog.faculty.edit', compact('faculty', 'educationalInstitutions'));
2024-02-07 16:30:49 +03:00
}
2024-02-07 19:27:25 +03:00
public function update(UpdateFacultyRequest $request, Faculty $faculty): RedirectResponse
2024-02-07 16:30:49 +03:00
{
$validated = $request->validated();
$faculty->name = $validated['name'];
$faculty->description = $validated['description'];
$faculty->position = $validated['position'];
2024-02-12 12:15:46 +03:00
$faculty->slug = $validated['slug'];
2024-02-07 16:30:49 +03:00
$faculty->educational_institution_id = $validated['educational_institution_id'];
$faculty->save();
return redirect()->route('faculties.index');
}
2024-02-07 19:27:25 +03:00
public function destroy(Faculty $faculty): RedirectResponse
2024-02-07 16:30:49 +03:00
{
if ($faculty->departments()->exists()) {
return back();
}
2024-02-07 16:30:49 +03:00
$faculty->delete();
return redirect()->route('faculties.index');
}
}