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.

95 lines
3.1 KiB

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\Grainbill;
  5. use App\Models\Summarie;
  6. use App\Models\Grain;
  7. class GrainbillController extends Controller
  8. {
  9. public function __construct()
  10. {
  11. $this->middleware('auth');
  12. }
  13. public function index()
  14. {
  15. $grainbills = Grainbill::get();
  16. foreach ($grainbills as $grainbill)
  17. {
  18. $beername = Summarie::where('beer_id', $grainbill->beer_id)->value('name');
  19. $grainname = Grain::where('id', $grainbill->grain_id)->value('name');
  20. $grainbill['beer'] = $beername;
  21. $grainbill['name'] = $grainname;
  22. }
  23. return view('grainbills.index')->with('grainbills', $grainbills);
  24. }
  25. public function store(Request $request){
  26. // validation
  27. $this->validate($request,[
  28. 'beer_id' => 'required',
  29. 'grain_id' => 'required',
  30. 'amount' => 'required',
  31. 'grain_bill' => 'required',
  32. ]);
  33. // create project
  34. $grain = new Grainbill;
  35. $lastID = Grainbill::orderBy('id','desc')->take(1)->value('id');
  36. $grain->id = number_format($lastID) + 1;
  37. $grain->beer_id = $request->input('beer_id');
  38. $grain->grain_id = $request->input('grain_id');
  39. $grain->amount = $request->input('amount');
  40. $grain->grain_bill = $request->input('grain_bill');
  41. $grain->save();
  42. return redirect('/grainbills')->with('success', 'Grain Bill Added');
  43. }
  44. public function edit($id){
  45. $grainbill = Grainbill::where('id', (int)$id)->first();
  46. $beers = Summarie::orderBy('name')->get();
  47. $selected_beer = $grainbill->beer_id;
  48. $grains = Grain::orderBy('name')->get();
  49. $selected_grain = $grainbill->grain_id;
  50. $data = array('grainbill'=>$grainbill, 'beers'=>$beers, 'grains'=>$grains, 'selected_beer'=>$selected_beer, 'selected_grain'=>$selected_grain);
  51. return view('grainbills.edit')->with($data);
  52. }
  53. public function update(Request $request, $id){
  54. // validation
  55. $this->validate($request,[
  56. 'beer_id' => 'required',
  57. 'grain_id' => 'required',
  58. 'amount' => 'required',
  59. 'grain_bill' => 'required',
  60. ]);
  61. $grain = Grainbill::where('id', (int)$id)->first();
  62. $grain->beer_id = $request->input('beer_id');
  63. $grain->grain_id = $request->input('grain_id');
  64. $grain->amount = $request->input('amount');
  65. $grain->grain_bill = $request->input('grain_bill');
  66. $grain->save();
  67. return redirect('/grainbills')->with('success', 'Grain Updated!');
  68. }
  69. public function create()
  70. {
  71. $beers = Summarie::orderBy('name')->get();
  72. $grains = Grain::orderBy('name')->get();
  73. $data = array('beers'=>$beers, 'grains'=>$grains);
  74. return view('grainbills.create')->with($data);
  75. }
  76. public function destroy($id)
  77. {
  78. $grainbill = Grainbill::find($id);
  79. $grainbill->delete();
  80. return redirect('/grainbills')->with('success', 'Grain deleted!');
  81. }
  82. }