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.

90 lines
2.8 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. return view('grainbills.edit')->with('grainbill', $grainbill);
  47. }
  48. public function update(Request $request, $id){
  49. // validation
  50. $this->validate($request,[
  51. 'beer_id' => 'required',
  52. 'grain_id' => 'required',
  53. 'amount' => 'required',
  54. 'grain_bill' => 'required',
  55. ]);
  56. $grain = Grainbill::where('id', (int)$id)->first();
  57. $grain->beer_id = $request->input('beer_id');
  58. $grain->grain_id = $request->input('grain_id');
  59. $grain->amount = $request->input('amount');
  60. $grain->grain_bill = $request->input('grain_bill');
  61. $grain->save();
  62. return redirect('/grainbills')->with('success', 'Grain Updated!');
  63. }
  64. public function create()
  65. {
  66. $beers = Summarie::orderBy('name')->value('beer_id', 'name');
  67. $grains = Grain::orderBy('name')->value('id', 'name');
  68. $data = array('beers'=>$beers, 'grains'=>$grains);
  69. return view('grainbills.create')->with($data);
  70. }
  71. public function destroy($id)
  72. {
  73. $grainbill = Grainbill::find($id);
  74. $grainbill->delete();
  75. return redirect('/grainbills')->with('success', 'Grain deleted!');
  76. }
  77. }