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.

92 lines
3.0 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\Models\Hopaddition;
  5. use App\Models\Hop;
  6. use App\Models\Summarie;
  7. class HopAdditionController extends Controller
  8. {
  9. public function __construct()
  10. {
  11. $this->middleware('auth');
  12. }
  13. public function index()
  14. {
  15. $hopadditions = Hopaddition::orderBy('id')->get();
  16. foreach ($hopadditions as $hopaddition)
  17. {
  18. $beername = Summarie::where('beer_id', $hopaddition->beer_id)->value('name');
  19. $hopname = Hop::where('id', $hopaddition->hop_id)->value('name');
  20. $hopaddition['beer'] = $beername;
  21. $hopaddition['hop'] = $hopname;
  22. }
  23. return view('hopadditions.index')->with('hopadditions', $hopadditions);
  24. }
  25. public function store(Request $request){
  26. // validation
  27. $this->validate($request,[
  28. 'beer_id' => 'required',
  29. 'hop_id' => 'required',
  30. 'amount' => 'required',
  31. 'timing' => 'required',
  32. ]);
  33. // create project
  34. $hop = new Hopaddition;
  35. $lastID = Hopaddition::orderBy('id','desc')->take(1)->value('id');
  36. $hop->id = number_format($lastID) + 1;
  37. $hop->beer_id = $request->input('beer_id');
  38. $hop->hop_id = $request->input('hop_id');
  39. $hop->amount = $request->input('amount');
  40. $hop->timing = $request->input('timing');
  41. $hop->save();
  42. return redirect('/hopadditions')->with('success', 'Hop Addition Added');
  43. }
  44. public function edit($id){
  45. $hopaddition = Hopaddition::where('id', (int)$id)->first();
  46. $beers = Summarie::orderBy('name')->get();
  47. $selected_beer = $hopaddition->beer_id;
  48. $hops = Hop::orderBy('name')->get();
  49. $selected_hop = $hopaddition->hop_id;
  50. $data = array('hopaddition'=>$hopaddition, 'beers'=>$beers, 'hops'=>$hops, 'selected_beer'=>$selected_beer, 'selected_hop'=>$selected_hop);
  51. return view('hopadditions.edit')->with($data);
  52. }
  53. public function update(Request $request, $id){
  54. // validation
  55. $this->validate($request,[
  56. 'beer_id' => 'required',
  57. 'hop_id' => 'required',
  58. 'amount' => 'required',
  59. 'timing' => 'required',
  60. ]);
  61. $hop = Hopaddition::where('id', (int)$id)->first();
  62. $hop->beer_id = $request->input('beer_id');
  63. $hop->hop_id = $request->input('hop_id');
  64. $hop->amount = $request->input('amount');
  65. $hop->timing = $request->input('timing');
  66. $hop->save();
  67. return redirect('/hopadditions')->with('success', 'Hop Addition Updated!');
  68. }
  69. public function create()
  70. {
  71. $beers = Summarie::orderBy('name')->get();
  72. $hops = Hop::orderBy('name')->get();
  73. $data = array('beers'=>$beers, 'hops'=>$hops);
  74. return view('hopadditions.create')->with($data);
  75. }
  76. public function destroy($id)
  77. {
  78. $hopaddition = Hopaddition::find($id);
  79. $hopaddition->delete();
  80. return redirect('/hopadditions')->with('success', 'Hop Addition deleted!');
  81. }
  82. }