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.

85 lines
2.5 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. 'name' => 'required',
  52. ]);
  53. $grain = Grainbill::where('id', (int)$id)->first();
  54. $grain->name = $request->input('name');
  55. $grain->beer_id = $request->input('beer_id');
  56. $grain->grain_id = $request->input('grain_id');
  57. $grain->amount = $request->input('amount');
  58. $grain->grain_bill = $request->input('grain_bill');
  59. $grain->save();
  60. return redirect('/grainbills')->with('success', 'Grain Updated!');
  61. }
  62. public function create()
  63. {
  64. return view('grainbills.create');
  65. }
  66. public function destroy($id)
  67. {
  68. $grainbill = Grainbill::find($id);
  69. $grainbill->delete();
  70. return redirect('/grainbills')->with('success', 'Grain deleted!');
  71. }
  72. }