80 lines
2.8 KiB
PHP
80 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\admin\Catalog;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\admin\Catalog\StoreFacultyRequest;
|
|
use App\Http\Requests\admin\Catalog\UpdateFacultyRequest;
|
|
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;
|
|
use Illuminate\Support\Str;
|
|
|
|
class FacultyController extends Controller
|
|
{
|
|
public function index(): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
|
|
{
|
|
$faculties = Faculty::all();
|
|
return view('admin.catalog.faculty.index', compact('faculties'));
|
|
}
|
|
|
|
public function create(): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
|
|
{
|
|
$educationalInstitutions = EducationalInstitution::pluck('name', 'id');
|
|
return view('admin.catalog.faculty.create', compact('educationalInstitutions'));
|
|
}
|
|
|
|
public function store(StoreFacultyRequest $request): RedirectResponse
|
|
{
|
|
$validated = $request->validated();
|
|
|
|
$slug = $validated['slug'] === null ? Str::slug($validated['name']) : $validated['slug'];
|
|
$faculty = new Faculty();
|
|
$faculty->name = $validated['name'];
|
|
$faculty->description = $validated['description'];
|
|
$faculty->position = $validated['position'];
|
|
$faculty->slug = $slug;
|
|
$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
|
|
{
|
|
return view('admin.catalog.faculty.show', compact('faculty'));
|
|
}
|
|
|
|
public function edit(Faculty $faculty): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
|
|
{
|
|
$educationalInstitutions = EducationalInstitution::pluck('name', 'id');
|
|
return view('admin.catalog.faculty.edit', compact('faculty', 'educationalInstitutions'));
|
|
}
|
|
|
|
public function update(UpdateFacultyRequest $request, Faculty $faculty): RedirectResponse
|
|
{
|
|
$validated = $request->validated();
|
|
|
|
$faculty->name = $validated['name'];
|
|
$faculty->description = $validated['description'];
|
|
$faculty->position = $validated['position'];
|
|
$faculty->slug = $validated['slug'];
|
|
$faculty->educational_institution_id = $validated['educational_institution_id'];
|
|
$faculty->save();
|
|
|
|
return redirect()->route('faculties.index');
|
|
}
|
|
|
|
public function destroy(Faculty $faculty): RedirectResponse
|
|
{
|
|
if ($faculty->departments()->exists()) {
|
|
return back();
|
|
}
|
|
$faculty->delete();
|
|
return redirect()->route('faculties.index');
|
|
}
|
|
}
|