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.

87 lines
2.7 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. return view('hopadditions.edit')->with('hopaddition', $hopaddition);
  47. }
  48. public function update(Request $request, $id){
  49. // validation
  50. $this->validate($request,[
  51. 'beer_id' => 'required',
  52. 'hop_id' => 'required',
  53. 'amount' => 'required',
  54. 'timing' => 'required',
  55. ]);
  56. $hop = Hopaddition::where('id', (int)$id)->first();
  57. $hop->beer_id = $request->input('beer_id');
  58. $hop->hop_id = $request->input('hop_id');
  59. $hop->amount = $request->input('amount');
  60. $hop->timing = $request->input('timing');
  61. $hop->save();
  62. return redirect('/hopadditions')->with('success', 'Hop Addition Updated!');
  63. }
  64. public function create()
  65. {
  66. $beers = Summarie::orderBy('name')->get();
  67. $hops = Hop::orderBy('name')->get();
  68. $data = array('beers'=>$beers, 'hops'=>$hops);
  69. return view('hopadditions.create')->with($data);
  70. }
  71. public function destroy($id)
  72. {
  73. $hopaddition = Hopaddition::find($id);
  74. $hopaddition->delete();
  75. return redirect('/hopadditions')->with('success', 'Hop Addition deleted!');
  76. }
  77. }