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.

125 lines
3.3 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\Models\Grain;
  5. use Illuminate\Support\Facades\DB;
  6. class GrainController extends Controller
  7. {
  8. public function __construct()
  9. {
  10. $this->middleware('auth');
  11. }
  12. public function index()
  13. {
  14. $grains = Grain::orderBy('name')->get();
  15. return view('grains.index')->with('grains', $grains);
  16. }
  17. public function store(Request $request){
  18. // validation
  19. $this->validate($request,[
  20. 'name' => 'required',
  21. ]);
  22. // create project
  23. $grain = new Grain;
  24. $lastID = Grain::orderBy('id','desc')->take(1)->value('id');
  25. $grain->id = number_format($lastID) + 1;
  26. $grain->name = $request->input('name');
  27. if ($request->input('flavour'))
  28. {
  29. $grain->flavour = $request->input('flavour');
  30. }
  31. if ($request->input('lintner'))
  32. {
  33. $grain->lintner = $request->input('lintner');
  34. }
  35. if ($request->input('winkol'))
  36. {
  37. $grain->winkol = $request->input('winkol');
  38. }
  39. if ($request->input('lovibond'))
  40. {
  41. $grain->lovibond = $request->input('lovibond');
  42. }
  43. if ($request->input('colour'))
  44. {
  45. $grain->colour = $request->input('colour');
  46. }
  47. if ($request->input('alternative'))
  48. {
  49. $altArr = array_map('intval',explode(',',$request->input('alternative')));
  50. $grain->alternative = $altArr;
  51. }
  52. if ($request->input('ebc'))
  53. {
  54. $grain->ebc = $request->input('ebc');
  55. }
  56. $grain->save();
  57. return redirect('/grains')->with('success', 'Grain Added');
  58. }
  59. public function edit($id){
  60. $grain = Grain::where('id', (int)$id)->first();
  61. return view('grains.edit')->with('grain', $grain);
  62. }
  63. public function update(Request $request, $id){
  64. // validation
  65. $this->validate($request,[
  66. 'name' => 'required',
  67. ]);
  68. $grain = Grain::where('id', (int)$id)->first();
  69. $grain->name = $request->input('name');
  70. if ($request->input('flavour'))
  71. {
  72. $grain->flavour = $request->input('flavour');
  73. }
  74. if ($request->input('lintner'))
  75. {
  76. $grain->lintner = $request->input('lintner');
  77. }
  78. if ($request->input('winkol'))
  79. {
  80. $grain->winkol = $request->input('winkol');
  81. }
  82. if ($request->input('lovibond'))
  83. {
  84. $grain->lovibond = $request->input('lovibond');
  85. }
  86. if ($request->input('colour'))
  87. {
  88. $grain->colour = $request->input('colour');
  89. }
  90. if ($request->input('alternative'))
  91. {
  92. $altArr = array_map('intval',explode(',',$request->input('alternative')));
  93. $grain->alternative = $altArr;
  94. }
  95. if ($request->input('ebc'))
  96. {
  97. $grain->ebc = $request->input('ebc');
  98. }
  99. $grain->save();
  100. return redirect('/grains')->with('success', 'Grain Updated!');
  101. }
  102. public function create()
  103. {
  104. return view('grains.create');
  105. }
  106. public function destroy($id)
  107. {
  108. $grain = Grain::find($id);
  109. $grain->delete();
  110. return redirect('/grains')->with('success', 'Grain deleted!');
  111. }
  112. }