v1.2.1 - Fixed Category for guides

This commit is contained in:
Anders 2020-09-14 08:57:31 +02:00
parent cab8ad604c
commit 580c38e940
3 changed files with 40 additions and 27 deletions

View File

@ -3,6 +3,7 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\GuidesCategory; use App\GuidesCategory;
use App\Guide;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Http\Response; use Illuminate\Http\Response;
@ -24,7 +25,7 @@ class GuidesCategoryController extends Controller
/** /**
* Display a listing of the resource. * Display a listing of the resource.
* *
* @return \Illuminate\Http\Response * @return Response
*/ */
public function index() public function index()
{ {
@ -60,7 +61,7 @@ class GuidesCategoryController extends Controller
if(!$saved) { if(!$saved) {
return redirect()->route("guides-category.store"); return redirect()->route("guides-category.store");
} else { } else {
$guides = GuidesCategory::query()->paginate(20); $guides = Guide::query()->paginate(20);
return redirect()->route("guides.index", ['guides' => $guides]); return redirect()->route("guides.index", ['guides' => $guides]);
} }
@ -69,64 +70,64 @@ class GuidesCategoryController extends Controller
/** /**
* Display the specified resource. * Display the specified resource.
* *
* @param \App\GuidesCategory $guidesCategory * @param int $id
* @return \Illuminate\Http\Response
*/ */
public function show(GuidesCategory $guidesCategory) public function show($id)
{ {
return Response::detect("guides-category.show", [ "guideCategory" => $guidesCategory]); return Response::detect("guides-category.show", $id);
} }
/** /**
* Show the form for editing the specified resource. * Show the form for editing the specified resource.
* *
* @param \App\GuidesCategory $guidesCategory * @param int $id
* @return \Illuminate\Http\Response * @return Response
*/ */
public function edit(GuidesCategory $guidesCategory) public function edit($id)
{ {
$guideCate = GuidesCategory::query()->where("id", "=", $guidesCategory->id)->first(); $guideCate = GuidesCategory::query()->where("id", "=", $id)->first();
dd($guideCate); return Response::detect("guides-category.edit", [ "guideCategory" => $guideCate]);
return redirect()->route("guides-category.edit", ["guideCategory" => $guideCate]);
} }
/** /**
* Update the specified resource in storage. * Update the specified resource in storage.
* *
* @param \Illuminate\Http\Request $request * @param \Illuminate\Http\Request $request
* @param \App\GuidesCategory $guidesCategory * @param int $id
* @return \Illuminate\Http\Response * @return \Illuminate\Http\Response
*/ */
public function update(Request $request, GuidesCategory $guidesCategory) public function update(Request $request, $id)
{ {
$data = $request->validate([ $data = $request->validate([
"guidesCategoryName" => "required|max:255", "guidesCategoryName" => "required|max:255",
]); ]);
$guideCate = Guide::query()->where("id", "=", $guidesCategory->id)->first(); $guideCate = GuidesCategory::query()->where("id", "=", $id)->first();
$guideCate->update($data); $guideCate->update($data);
$saved = $guideCate->save(); $saved = $guideCate->save();
if(!$saved){ if(!$saved){
return redirect()->route("guides-category.update", [ "guideCategory" => $guide ]); return redirect()->route("guides-category.edit", $id);
}else{ }else{
$guides = Guide::query()->paginate(20); $guides = Guide::query()->paginate(20);
return redirect()->route("guides.index", ['guidesCategory' => $guides]); return redirect()->route("guides.index", ['guides' => $guides]);
} }
} }
/** /**
* Remove the specified resource from storage. * Remove the specified resource from storage.
* *
* @param \App\GuidesCategory $guidesCategory * @param int $id
* @return \Illuminate\Http\Response * @return \Illuminate\Http\Response
*/ */
public function destroy(GuidesCategory $guidesCategory) public function destroy($id)
{ {
$guide = Guide::find($guidesCategory); //$guidesCategory = $id $guideCategory = GuidesCategory::find($id);
$guide->delete(); $guides = Guide::query()->where('guide_category_id', '=', $id);
$guides->delete();
$guideCategory->delete();
return redirect()->route("guides.index"); return redirect()->route("guides.index");
} }

View File

@ -6,17 +6,17 @@
@endsection @endsection
@section("path") @section("path")
<a href="{{route('guides-category.edit', ['guidesCategory' => $guideCategory ])}}" class="text-white">Rediger kategori</a> / <a href="{{route('guides-category.edit', $guideCategory )}}" class="text-white">Rediger kategori</a> /
@endsection @endsection
@section("content") @section("content")
<h1>Rediger vejlednings kategori</h1> <h1>Rediger vejlednings kategori</h1>
<form method="post" action="{{ route("guides-category.update", ['guidesCategory' => $guideCategory ]) }}"> <form method="post" action="{{ route("guides-category.update", $guideCategory) }}">
@csrf @csrf
@method("put") @method("put")
<label for="name">Kategori navn:</label> <label for="name">Kategori navn:</label>
<label hidden id="error" for="errormesseages">kategorien findes allerede</label> <label hidden id="error" for="errormesseages">Kategorien findes allerede</label>
<input type="text" name="name" id="name" value="{{ $guideCategory->name }}" required> <input type="text" name="guidesCategoryName" id="guidesCategoryName" value="{{ $guideCategory->guidesCategoryName }}" required>
<input type="submit" id="disable" class="btn btn-dark text-white" value="Rediger"> <input type="submit" id="disable" class="btn btn-dark text-white" value="Rediger">
</form> </form>
@endsection @endsection
@ -33,7 +33,7 @@
//Dont delete me, if the user reloads the pages with text in the text-box this code runs. //Dont delete me, if the user reloads the pages with text in the text-box this code runs.
$(document).ready(function () { $(document).ready(function () {
$value = $('#name').val(); $value = $('#guidesCategoryName').val();
$.ajax({ $.ajax({
type: 'get', type: 'get',
url: '{{route('guides-category.nameCheckUpdate')}}', url: '{{route('guides-category.nameCheckUpdate')}}',
@ -57,7 +57,7 @@
}); });
}); });
$('#name').on('keyup', function () { $('#guidesCategoryName').on('keyup', function () {
$value = $(this).val(); $value = $(this).val();
$.ajax({ $.ajax({
type: 'get', type: 'get',

View File

@ -65,6 +65,18 @@
@foreach(\App\GuidesCategory::query()->get() as $guideCategory) @foreach(\App\GuidesCategory::query()->get() as $guideCategory)
<tr> <tr>
<td>{{$guideCategory->guidesCategoryName}}</td> <td>{{$guideCategory->guidesCategoryName}}</td>
@if(auth()->user()->can('guides.edit'))
<td><a href="{{ route("guides-category.edit", $guideCategory) }}"><img class="w-100" src="{{ asset('/images/icons/pencil-dark.svg') }}" alt="Update2"></a></td>
@endif
@if(auth()->user()->can('guides.delete'))
<td><form method="post" action="{{ route("guides-category.destroy", $guideCategory) }}" class="w-100 nostyle">
@csrf
@method("delete")
<button onclick="return confirm('Are you sure you want to delete? You will also delete all guides with this category!');" class="w-100 nostyle" type="submit"><img class="w-100 cursor-pointer" src="{{ asset('/images/icons/trashcan-dark.svg') }}" alt="Delete"></button>
</form>
</td>
@endif
</tr> </tr>
@endforeach @endforeach
</table> </table>