Bulletinboard/app/Http/Controllers/OccupationController.php

118 lines
2.8 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Occupation;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Redirector;
class OccupationController extends Controller
{
/**
* Display a listing of the resource.
*
* @param Request $request
* @return View
*/
public function index(Request $request): View
{
$occupations = Occupation::all();
return view("occupation.index", [ "occupations" => $occupations]);
}
/**
* Show the form for creating a new resource.
*
* @return View
*/
public function create(): View
{
return view("occupation.create");
}
/**
* Store a newly created resource in storage.
*
* @param Request $request
* @return RedirectResponse
*/
public function store(Request $request) : RedirectResponse
{
$rawOccupation = $request->validate([
'name' => 'required|string'
]);
$Occupation = new Occupation($rawOccupation);
if($Occupation->save()) {
return redirect()->route("index");
}
return redirect()->route("error");
}
/**
* Show the form for editing the specified resource.
*
* @param Occupation $occupation
* @return Response
*/
public function edit(Occupation $occupation)
{
return \view("occupation.show-occupation")->with([
"occupation" => $occupation
]);
}
/**
* Update the specified resource in storage.
*
* @param Request $request
* @param Occupation $occupation
* @return View
*/
public function update(Request $request, Occupation $occupation): View
{
$data = $request->validate([
'name' => 'required|string'
]);
$occupation->update($data);
if(!$occupation->save()){
return view("occupation.show-occupation",
[
"occupation" => $occupation,
"error" => "occupation was not updated"
]);
}else{
return view("occupation.show-occupation",
[
"occupation" => $occupation,
"success" => "updated occupation"
]);
}
}
/**
* Remove the specified resource from storage.
*
* @param Occupation $occupation
* @return RedirectResponse
*/
public function destroy(Occupation $occupation): RedirectResponse
{
$occupation->post()->delete();
if($occupation->delete()){
return redirect()->route("index");
}
return redirect()->route("error");
}
}