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.

132 lines
3.7 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
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\Models\Summarie;
  5. use Illuminate\Support\Facades\DB;
  6. class SummarieController extends Controller
  7. {
  8. public function __construct()
  9. {
  10. $this->middleware('auth');
  11. }
  12. public function index()
  13. {
  14. $beers = Summarie::orderBy('name')->get();
  15. foreach ($beers as $beer)
  16. {
  17. $brewery = DB::table('breweries')->where('id', $beer->beer_id)->value('name');
  18. $beer['brewery'] = $brewery;
  19. }
  20. return view('beers.index')->with('beers', $beers);
  21. }
  22. public function store(Request $request){
  23. // validation
  24. $this->validate($request,[
  25. 'name' => 'required',
  26. 'type' => 'required',
  27. 'abv' => 'required',
  28. 'fg' => 'required',
  29. 'keywords' => 'required',
  30. 'og' => 'required',
  31. 'source' => 'required',
  32. 'batch' => 'required',
  33. 'beer_id' => 'required',
  34. 'brewery_id' => 'required'
  35. ]);
  36. // create project
  37. $beer = new Summarie;
  38. $lastID = Summarie::orderBy('beer_id','desc')->value('beer_id');
  39. $beer->beer_id = number_format($lastID) + 1;
  40. $beer->name = $request->input('name');
  41. $beer->type = $request->input('type');
  42. $beer->abv = $request->input('abv');
  43. $beer->fg = $request->input('fg');
  44. $beer->keywords = '{'.$request->input('keywords').'}';
  45. if ($request->mine)
  46. {
  47. $beer->mine = true;
  48. } else {
  49. $beer->mine = false;
  50. }
  51. $beer->og = $request->input('og');
  52. $beer->source = $request->input('source');
  53. $beer->batch = $request->input('batch');
  54. $beer->beer_id = $request->input('beer_id');
  55. $beer->brewery_id = $request->input('brewery_id');
  56. if ($request->input('notes'))
  57. {
  58. $beer->notes = $request->input('notes');
  59. }
  60. $beer->save();
  61. return redirect('/beers')->with('success', 'Beer Added');
  62. }
  63. public function edit($id){
  64. $beer = Summarie::where('beer_id', (int)$id)->first();
  65. return view('beers.edit')->with('beer', $beer);
  66. }
  67. public function update(Request $request, $id)
  68. {
  69. $request->validate([
  70. 'name' => 'required',
  71. 'type' => 'required',
  72. 'abv' => 'required',
  73. 'fg' => 'required',
  74. 'keywords' => 'required',
  75. 'og' => 'required',
  76. 'source' => 'required',
  77. 'batch' => 'required',
  78. 'beer_id' => 'required',
  79. 'brewery_id' => 'required'
  80. ]);
  81. $beer = Summarie::where('beer_id', $id)->first();
  82. $beer->name = $request->input('name');
  83. $beer->type = $request->input('type');
  84. $beer->abv = $request->input('abv');
  85. $beer->fg = $request->input('fg');
  86. $beer->keywords = '{'.$request->input('keywords').'}';
  87. if ($request->mine)
  88. {
  89. $beer->mine = true;
  90. } else {
  91. $beer->mine = false;
  92. }
  93. $beer->og = $request->input('og');
  94. $beer->source = $request->input('source');
  95. $beer->batch = $request->input('batch');
  96. $beer->beer_id = $request->input('beer_id');
  97. $beer->brewery_id = $request->input('beer_id');
  98. if ($request->input('notes'))
  99. {
  100. $beer->notes = $request->input('notes');
  101. }
  102. $beer->save();
  103. return redirect('/beers')->with('success', 'Beer updated!');
  104. }
  105. public function create()
  106. {
  107. return view('beers.create');
  108. }
  109. public function destroy($id)
  110. {
  111. $beer = Summarie::find($id);
  112. $beer->delete();
  113. return redirect('/beers')->with('success', 'Beer deleted!');
  114. }
  115. }