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.

103 lines
3.5 KiB

3 years ago
3 years ago
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\Fermentation;
  5. use App\Models\Yeast;
  6. use App\Models\Summarie;
  7. class FermentationController extends Controller
  8. {
  9. public function __construct()
  10. {
  11. $this->middleware('auth');
  12. }
  13. public function index()
  14. {
  15. $fermentations = Fermentation::get();
  16. foreach ($fermentations as $fermentation)
  17. {
  18. $beername = Summarie::where('beer_id', $fermentation->beer_id)->value('name');
  19. $yeastname = Yeast::where('id', $fermentation->yeast_id)->value('name');
  20. $fermentation['beer'] = $beername;
  21. $fermentation['name'] = $yeastname;
  22. }
  23. return view('fermentations.index')->with('fermentations', $fermentations);
  24. }
  25. public function store(Request $request){
  26. // validation
  27. $this->validate($request,[
  28. 'beer_id' => 'required',
  29. 'yeast_id' => 'required',
  30. 'temperature' => 'required',
  31. 'duration' => 'required',
  32. ]);
  33. // create project
  34. $yeast = new Fermentation;
  35. $lastID = Fermentation::orderBy('id','desc')->take(1)->value('id');
  36. $yeast->id = number_format($lastID) + 1;
  37. $yeast->beer_id = $request->input('beer_id');
  38. $yeast->yeast_id = $request->input('yeast_id');
  39. $yeast->temperature = $request->input('temperature');
  40. $yeast->duration = $request->input('duration');
  41. if ($request->input('alternative'))
  42. {
  43. $yeast->alternative = $request->input('alternative');
  44. }
  45. $yeast->save();
  46. return redirect('/fermentations')->with('success', 'Fermentation Step Added');
  47. }
  48. public function edit($id){
  49. $fermentation = Fermentation::where('id', (int)$id)->first();
  50. $beers = Summarie::orderBy('name')->get();
  51. $selected_beer = $fermentation->beer_id;
  52. $yeasts = Yeast::orderBy('name')->get();
  53. $selected_yeast = $fermentation->yeast_id;
  54. $selected_alt = $fermentation->alternative;
  55. $data = array('fermentation'=>$fermentation, 'beers'=>$beers, 'yeasts'=>$yeasts, 'selected_beer'=>$selected_beer, 'selected_yeast'=>$selected_yeast, 'selected_alt'=>$selected_alt);
  56. return view('fermentations.edit')->with($data);
  57. }
  58. public function update(Request $request, $id){
  59. // validation
  60. $this->validate($request,[
  61. 'beer_id' => 'required',
  62. 'yeast_id' => 'required',
  63. 'temperature' => 'required',
  64. 'duration' => 'required',
  65. ]);
  66. $yeast = Fermentation::where('id', (int)$id)->first();
  67. $yeast->beer_id = $request->input('beer_id');
  68. $yeast->yeast_id = $request->input('yeast_id');
  69. $yeast->temperature = $request->input('temperature');
  70. $yeast->duration = $request->input('duration');
  71. if ($request->input('alternative'))
  72. {
  73. $yeast->alternative = $request->input('alternative');
  74. }
  75. $yeast->save();
  76. return redirect('/fermentations')->with('success', 'Fermentation Step Updated!');
  77. }
  78. public function create()
  79. {
  80. $beers = Summarie::orderBy('name')->get();
  81. $yeasts = Yeast::orderBy('name')->get();
  82. $data = array('beers'=>$beers, 'yeasts'=>$yeasts);
  83. return view('fermentations.create')->with($data);
  84. }
  85. public function destroy($id)
  86. {
  87. $fermentation = Fermentation::find($id);
  88. $fermentation->delete();
  89. return redirect('/fermentations')->with('success', 'Fermentation Step deleted!');
  90. }
  91. }