2024-02-08 15:59:44 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Catalog;
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
use App\Http\Requests\StoreDepartmentRequest;
|
|
|
|
use App\Http\Requests\UpdateDepartmentRequest;
|
|
|
|
use App\Models\Department;
|
|
|
|
use App\Models\Faculty;
|
|
|
|
use Illuminate\Contracts\View\Factory;
|
|
|
|
use Illuminate\Contracts\View\View;
|
|
|
|
use Illuminate\Foundation\Application;
|
|
|
|
use Illuminate\Http\RedirectResponse;
|
|
|
|
|
|
|
|
class DepartmentController extends Controller
|
|
|
|
{
|
|
|
|
public function index(): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
|
|
|
|
{
|
|
|
|
$departments = Department::all();
|
|
|
|
return view('catalog.department.index', compact('departments'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function create(): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
|
|
|
|
{
|
|
|
|
$faculties = Faculty::pluck('name', 'id');
|
|
|
|
return view('catalog.department.create', compact('faculties'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function store(StoreDepartmentRequest $request): RedirectResponse
|
|
|
|
{
|
|
|
|
$validated = $request->validated();
|
|
|
|
|
|
|
|
$department = new Department();
|
|
|
|
$department->name = $validated['name'];
|
|
|
|
$department->description = $validated['description'];
|
|
|
|
$department->position = $validated['position'];
|
|
|
|
$department->faculty_id = $validated['faculty_id'];
|
|
|
|
$department->save();
|
|
|
|
|
|
|
|
return redirect()->route('departments.index');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function show(Department $department): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
|
|
|
|
{
|
|
|
|
return view('catalog.department.show', compact('department'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function edit(Department $department): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
|
|
|
|
{
|
|
|
|
$faculties = Faculty::pluck('name', 'id');
|
|
|
|
return view('catalog.department.edit', compact('department', 'faculties'));
|
|
|
|
}
|
|
|
|
|
2024-02-10 15:14:02 +03:00
|
|
|
public function update(UpdateDepartmentRequest $request, Department $department): RedirectResponse
|
2024-02-08 15:59:44 +03:00
|
|
|
{
|
|
|
|
$validated = $request->validated();
|
|
|
|
|
|
|
|
$department->name = $validated['name'];
|
|
|
|
$department->description = $validated['description'];
|
|
|
|
$department->position = $validated['position'];
|
|
|
|
$department->faculty_id = $validated['faculty_id'];
|
|
|
|
$department->save();
|
|
|
|
|
|
|
|
return redirect()->route('departments.index');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function destroy(Department $department): RedirectResponse
|
|
|
|
{
|
2024-02-10 15:14:02 +03:00
|
|
|
if ($department->directions()->exists()) {
|
|
|
|
return back();
|
|
|
|
}
|
2024-02-08 15:59:44 +03:00
|
|
|
$department->delete();
|
|
|
|
return redirect()->route('departments.index');
|
|
|
|
}
|
|
|
|
}
|