|
|
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request; use App\Models\Summarie; use Illuminate\Support\Facades\DB;
class SummarieController extends Controller { public function __construct() { $this->middleware('auth'); }
public function index() { $beers = Summarie::orderBy('name')->get(); foreach ($beers as $beer) { $brewery = DB::table('breweries')->where('id', $beer->beer_id)->value('name'); $beer['brewery'] = $brewery; } return view('beers.index')->with('beers', $beers); }
public function store(Request $request){ // validation
$this->validate($request,[ 'name' => 'required', 'type' => 'required', 'abv' => 'required', 'fg' => 'required', 'keywords' => 'required', 'og' => 'required', 'source' => 'required', 'batch' => 'required', 'beer_id' => 'required', 'brewery_id' => 'required' ]);
// create project
$beer = new Summarie; $beer->beer_id = $request->input('beer_id'); $beer->name = $request->input('name'); $beer->type = $request->input('type'); $beer->abv = $request->input('abv'); $beer->fg = $request->input('fg'); $beer->keywords = '{'.$request->input('keywords').'}'; if ($request->mine) { $beer->mine = true; } else { $beer->mine = false; } $beer->og = $request->input('og'); $beer->source = $request->input('source'); $beer->batch = $request->input('batch'); $beer->beer_id = $request->input('beer_id'); $beer->brewery_id = $request->input('brewery_id'); if ($request->input('notes')) { $beer->notes = $request->input('notes'); } $beer->save();
return redirect('/beers')->with('success', 'Beer Added'); }
public function edit($id){ $beer = Summarie::where('beer_id', (int)$id)->first(); return view('beers.edit')->with('beer', $beer); }
public function update(Request $request, $id) { $request->validate([ 'name' => 'required', 'type' => 'required', 'abv' => 'required', 'fg' => 'required', 'keywords' => 'required', 'og' => 'required', 'source' => 'required', 'batch' => 'required', 'beer_id' => 'required', 'brewery_id' => 'required' ]);
$beer = Summarie::where('beer_id', $id)->first(); $beer->name = $request->input('name'); $beer->type = $request->input('type'); $beer->abv = $request->input('abv'); $beer->fg = $request->input('fg'); $beer->keywords = '{'.$request->input('keywords').'}'; if ($request->mine) { $beer->mine = true; } else { $beer->mine = false; } $beer->og = $request->input('og'); $beer->source = $request->input('source'); $beer->batch = $request->input('batch'); $beer->beer_id = $request->input('beer_id'); $beer->brewery_id = $request->input('beer_id'); if ($request->input('notes')) { $beer->notes = $request->input('notes'); } $beer->save();
return redirect('/beers')->with('success', 'Beer updated!'); } public function create() { $lastID = Summarie::orderBy('beer_id','desc')->first()->value('beer_id'); $beer_id = number_format($lastID) + 1; return view('beers.create')->with('beer_id', $beer_id);
} public function destroy($id) { $beer = Summarie::find($id); $beer->delete();
return redirect('/beers')->with('success', 'Beer deleted!'); }
}
|