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.

79 lines
2.0 KiB

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\Brewerie;
  5. class BrewerieController extends Controller
  6. {
  7. public function __construct()
  8. {
  9. $this->middleware('auth');
  10. }
  11. public function index()
  12. {
  13. $project = Brewerie::orderBy('name')->get();
  14. return view('breweries.index')->with('breweries', $project);
  15. }
  16. public function store(Request $request){
  17. // validation
  18. $this->validate($request,[
  19. 'name' => 'required',
  20. ]);
  21. // create project
  22. $brewerie = new Brewerie;
  23. $lastID = Brewerie::orderBy('id','desc')->take(1)->value('id');
  24. $brewerie->id = number_format($lastID) + 1;
  25. $brewerie->name = $request->input('name');
  26. if ($request->input('place'))
  27. {
  28. $brewerie->place = $request->input('place');
  29. }
  30. if ($request->input('country'))
  31. {
  32. $brewerie->country = $request->input('country');
  33. }
  34. $brewerie->save();
  35. return redirect('/breweries')->with('success', 'Brewery Added');
  36. }
  37. public function edit($id){
  38. $brewery = Brewerie::where('id', (int)$id)->first();
  39. return view('breweries.edit')->with('brewery', $brewery);
  40. }
  41. public function update(Request $request, $id)
  42. {
  43. $request->validate([
  44. 'name'=>'required',
  45. 'place'=>'required',
  46. 'country'=>'required'
  47. ]);
  48. $brewery = Brewerie::where('id', $id)->first();
  49. $brewery->name = $request->get('name');
  50. $brewery->place = $request->get('place');
  51. $brewery->country = $request->get('country');
  52. $brewery->save();
  53. return redirect('/breweries')->with('success', 'Brewery updated!');
  54. }
  55. public function create()
  56. {
  57. return view('breweries.create');
  58. }
  59. public function destroy($id)
  60. {
  61. $brewery = Brewerie::find($id);
  62. $brewery->delete();
  63. return redirect('/breweries')->with('success', 'Brewery deleted!');
  64. }
  65. }