Roman_applicant-site/app/Http/Controllers/admin/Catalog/DirectionController.php

95 lines
3.4 KiB
PHP

<?php
namespace App\Http\Controllers\admin\Catalog;
use App\Http\Controllers\Controller;
use App\Http\Requests\admin\Catalog\StoreDirectionRequest;
use App\Http\Requests\admin\Catalog\UpdateDirectionRequest;
use App\Models\Department;
use App\Models\Direction;
use App\Models\EducationLevel;
use App\Models\Faculty;
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
use Illuminate\Foundation\Application;
use Illuminate\Http\RedirectResponse;
class DirectionController extends Controller
{
public function index(): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
{
$directions = Direction::all();
return view('admin.catalog.direction.index', compact('directions'));
}
public function create(): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
{
$levels = EducationLevel::pluck('name', 'id');
$departments = Department::pluck('name', 'id');
return view('admin.catalog.direction.create', compact('departments', 'levels'));
}
public function store(StoreDirectionRequest $request): RedirectResponse
{
$validated = $request->validated();
$direction = new Direction();
$direction->name = $validated['name'];
$direction->description = $validated['description'];
$direction->position = $validated['position'];
$direction->slug = $validated['slug'];
$direction->code = $validated['code'];
$direction->education_level_id = $validated['education_level_id'];
$direction->department_id = $validated['department_id'];
$direction->save();
return redirect()->route('directions.index');
}
public function show(Direction $direction): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
{
$department = $direction->department;
$faculty = Faculty::find($department->faculty->id);
$educationalInstitution = $faculty->educationalInstitution;
return view(
'admin.catalog.direction.show',
compact(
'direction',
'educationalInstitution',
'faculty',
'department',
)
);
}
public function edit(Direction $direction): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
{
$levels = EducationLevel::pluck('name', 'id');
$departments = Department::pluck('name', 'id');
return view('admin.catalog.direction.edit', compact('direction', 'departments', 'levels'));
}
public function update(UpdateDirectionRequest $request, Direction $direction): RedirectResponse
{
$validated = $request->validated();
$direction = new Direction();
$direction->name = $validated['name'];
$direction->description = $validated['description'];
$direction->position = $validated['position'];
$direction->slug = $validated['slug'];
$direction->code = $validated['code'];
$direction->education_level_id = $validated['education_level_id'];
$direction->department_id = $validated['department_id'];
$direction->save();
return redirect()->route('directions.index');
}
public function destroy(Direction $direction): RedirectResponse
{
$direction->delete();
return redirect()->route('directions.index');
}
}