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.

123 lines
3.2 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
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\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. $grain->alternative = '{'.$request->input('alternative').'}';
  50. }
  51. if ($request->input('ebc'))
  52. {
  53. $grain->ebc = $request->input('ebc');
  54. }
  55. $grain->save();
  56. return redirect('/grains')->with('success', 'Grain Added');
  57. }
  58. public function edit($id){
  59. $grain = Grain::where('id', (int)$id)->first();
  60. return view('grains.edit')->with('grain', $grain);
  61. }
  62. public function update(Request $request, $id){
  63. // validation
  64. $this->validate($request,[
  65. 'name' => 'required',
  66. ]);
  67. $grain = Grain::where('id', (int)$id)->first();
  68. $grain->name = $request->input('name');
  69. if ($request->input('flavour'))
  70. {
  71. $grain->flavour = $request->input('flavour');
  72. }
  73. if ($request->input('lintner'))
  74. {
  75. $grain->lintner = $request->input('lintner');
  76. }
  77. if ($request->input('winkol'))
  78. {
  79. $grain->winkol = $request->input('winkol');
  80. }
  81. if ($request->input('lovibond'))
  82. {
  83. $grain->lovibond = $request->input('lovibond');
  84. }
  85. if ($request->input('colour'))
  86. {
  87. $grain->colour = $request->input('colour');
  88. }
  89. if ($request->input('alternative'))
  90. {
  91. $grain->alternative = '{'.$request->input('alternative').'}';
  92. }
  93. if ($request->input('ebc'))
  94. {
  95. $grain->ebc = $request->input('ebc');
  96. }
  97. $grain->save();
  98. return redirect('/grains')->with('success', 'Grain Updated!');
  99. }
  100. public function create()
  101. {
  102. return view('grains.create');
  103. }
  104. public function destroy($id)
  105. {
  106. $grain = Grain::find($id);
  107. $grain->delete();
  108. return redirect('/grains')->with('success', 'Grain deleted!');
  109. }
  110. }