Ekapp/skolehjem/app/Http/Controllers/GuidesCategoryController.php

152 lines
4.1 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\GuidesCategory;
use App\Guide;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
class GuidesCategoryController extends Controller
{
public function __construct()
{
$this->middleware([ "auth" ]);
$this->middleware([ "lang" ]);
$this->middleware([ "check.auth:guides.show" ])->only("show", "index");
$this->middleware([ "check.auth:guides.create" ])->only("create", "store");
$this->middleware([ "check.auth:guides.edit" ])->only("edit", "update");
$this->middleware([ "check.auth:guides.delete" ])->only("delete");
}
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
$guideCate = GuidesCategory::query()->get();
return Response::detect("guides-category.index", [ "guidesCategory" => $guideCate]);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return Response::detect("guides-category.create");
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$requestGuideCategory = $request->validate([
"guidesCategoryName" => "required|max:255",
]);
$guideCate = new GuidesCategory($requestGuideCategory);
$saved = $guideCate->save();
if(!$saved) {
return redirect()->route("guides-category.store");
} else {
$guides = Guide::query()->get();
return redirect()->route("guides.index", ['guides' => $guides]);
}
}
/**
* Display the specified resource.
*
* @param int $id
*/
public function show($id)
{
return Response::detect("guides-category.show", $id);
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return Response
*/
public function edit($id)
{
$guideCate = GuidesCategory::query()->where("id", "=", $id)->first();
return Response::detect("guides-category.edit", [ "guideCategory" => $guideCate]);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$data = $request->validate([
"guidesCategoryName" => "required|max:255",
]);
$guideCate = GuidesCategory::query()->where("id", "=", $id)->first();
$guideCate->update($data);
$saved = $guideCate->save();
if(!$saved){
return redirect()->route("guides-category.edit", $id);
}else{
$guides = Guide::query()->get();
return redirect()->route("guides.index", ['guides' => $guides]);
}
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$guideCategory = GuidesCategory::find($id);
$guides = Guide::query()->where('guide_category_id', '=', $id);
$guides->delete();
$guideCategory->delete();
return redirect()->route("guides.index");
}
//Used for checking if the currently typed Guide Category name is unique. Create version
public function nameCheck(Request $request) {
$guide = GuidesCategory::query()->where('guidesCategoryName', 'LIKE',$request->nameCheck)->get();
if(count($guide) > 0 && $request->nameCheck !== NULL){
return 1;
}
}
//Used for checking if the currently typed Guide Category name is unique. Edit version
public function nameCheckUpdate(Request $request) {
$guide = GuidesCategory::query()->where('guidesCategoryName', 'LIKE',$request->nameCheck)->where('id', '!=', $request->id)->get();
if(count($guide) > 0 && $request->nameCheck !== NULL){
return 1;
}
}
}