My beer compendium
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

95 lines
3.1 KiB

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Grainbill;
use App\Models\Summarie;
use App\Models\Grain;
class GrainbillController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
$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);
}
public function store(Request $request){
// validation
$this->validate($request,[
'beer_id' => 'required',
'grain_id' => 'required',
'amount' => 'required',
'grain_bill' => 'required',
]);
// create project
$grain = new Grainbill;
$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');
$grain->amount = $request->input('amount');
$grain->grain_bill = $request->input('grain_bill');
$grain->save();
return redirect('/grainbills')->with('success', 'Grain Bill Added');
}
public function edit($id){
$grainbill = Grainbill::where('id', (int)$id)->first();
$beers = Summarie::orderBy('name')->get();
$selected_beer = $grainbill->beer_id;
$grains = Grain::orderBy('name')->get();
$selected_grain = $grainbill->grain_id;
$data = array('grainbill'=>$grainbill, 'beers'=>$beers, 'grains'=>$grains, 'selected_beer'=>$selected_beer, 'selected_grain'=>$selected_grain);
return view('grainbills.edit')->with($data);
}
public function update(Request $request, $id){
// validation
$this->validate($request,[
'beer_id' => 'required',
'grain_id' => 'required',
'amount' => 'required',
'grain_bill' => 'required',
]);
$grain = Grainbill::where('id', (int)$id)->first();
$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()
{
$beers = Summarie::orderBy('name')->get();
$grains = Grain::orderBy('name')->get();
$data = array('beers'=>$beers, 'grains'=>$grains);
return view('grainbills.create')->with($data);
}
public function destroy($id)
{
$grainbill = Grainbill::find($id);
$grainbill->delete();
return redirect('/grainbills')->with('success', 'Grain deleted!');
}
}