From 15320357248bf2a0790196544b060000b36ba9af Mon Sep 17 00:00:00 2001 From: jk Date: Tue, 22 Jun 2021 13:12:15 +0100 Subject: [PATCH] grainbills refactor mkI --- app/Http/Controllers/GrainbillController.php | 53 ++++++++++++++--- resources/views/grainbills.blade.php | 60 +------------------- resources/views/grainbills/create.blade.php | 40 +++++++++++++ resources/views/grainbills/edit.blade.php | 39 +++++++++++++ resources/views/grainbills/index.blade.php | 50 ++++++++++++++++ resources/views/hops/create.blade.php | 4 +- routes/web.php | 3 +- 7 files changed, 180 insertions(+), 69 deletions(-) diff --git a/app/Http/Controllers/GrainbillController.php b/app/Http/Controllers/GrainbillController.php index df19774..de84ccd 100644 --- a/app/Http/Controllers/GrainbillController.php +++ b/app/Http/Controllers/GrainbillController.php @@ -4,6 +4,8 @@ namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Grainbill; +use App\Models\Summarie; +use App\Models\Grain; class GrainbillController extends Controller { @@ -14,8 +16,15 @@ class GrainbillController extends Controller public function index() { - $project = Grainbill::get(); - return view('grainbills')->with('grainbills', $project); + $grainbills = Grainbill::get(); + foreach ($grainbills as $grainbill) + { + $beername = Summarie::where('beer_id', $grainbill->beer_id)->value('name'); + $grainname = Grain::where('id', $grainbill->grain_id)->value('name'); + $grainbill['beer'] = $beername; + $grainbill['name'] = $grainname; + } + return view('grainbills.index')->with('grainbills', $grainbills); } @@ -23,15 +32,14 @@ class GrainbillController extends Controller // validation $this->validate($request,[ 'beer_id' => 'required', - 'grain_id' => 'required', - 'amount' => 'required', - 'grain_bill' => 'required', + 'grain_id' => 'required', + 'amount' => 'required', + 'grain_bill' => 'required', ]); - // create project $grain = new Grainbill; - $lastID = Grainbill::orderBy('id','desc')->value('id'); + $lastID = Grainbill::orderBy('id','desc')->take(1)->value('id'); $grain->id = number_format($lastID) + 1; $grain->beer_id = $request->input('beer_id'); $grain->grain_id = $request->input('grain_id'); @@ -41,6 +49,37 @@ class GrainbillController extends Controller return redirect('/grainbills')->with('success', 'Grain Bill Added'); } + public function edit($id){ + $grainbill = Grainbill::where('id', (int)$id)->first(); + return view('grainbills.edit')->with('grainbill', $grainbill); + } + + public function update(Request $request, $id){ + // validation + $this->validate($request,[ + 'name' => 'required', + ]); + + $grain = Grainbill::where('id', (int)$id)->first(); + $grain->name = $request->input('name'); + $grain->beer_id = $request->input('beer_id'); + $grain->grain_id = $request->input('grain_id'); + $grain->amount = $request->input('amount'); + $grain->grain_bill = $request->input('grain_bill'); + $grain->save(); + + return redirect('/grainbills')->with('success', 'Grain Updated!'); + } + public function create() + { + return view('grainbills.create'); + } + public function destroy($id) + { + $grainbill = Grainbill::find($id); + $grainbill->delete(); + return redirect('/grainbills')->with('success', 'Grain deleted!'); + } } diff --git a/resources/views/grainbills.blade.php b/resources/views/grainbills.blade.php index 2c505f7..9ede88a 100644 --- a/resources/views/grainbills.blade.php +++ b/resources/views/grainbills.blade.php @@ -1,64 +1,8 @@ @extends('layouts.app') @section('content') -
-
-
-
-@include('inc.navbar') -
Home -> {{ __('Grain Bills') }}Go Back
- -
-@if (Auth::user()->isAdmin()) - -
-@endif - - @include('inc.messages') -
-
-
-
-
- -