GroupController.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Category;
  4. use App\Group;
  5. use Illuminate\Http\Request;
  6. use File;
  7. class GroupController extends Controller
  8. {
  9. protected $rules = [
  10. // 'name' => ['required']
  11. ];
  12. public function index(Category $category)
  13. {
  14. $groups = Group::where('category_id', '=', $category->id)->orderBy('position')->get();
  15. return view('groups.index',compact('groups', 'category'));
  16. }
  17. /**
  18. * Show the form for creating a new resource.
  19. *
  20. * @return \Illuminate\Http\Response
  21. */
  22. public function create(Category $category)
  23. {
  24. return view('groups.create', compact('category'));
  25. }
  26. /**
  27. * Store a newly created resource in storage.
  28. *
  29. * @param \Illuminate\Http\Request $request
  30. * @return \Illuminate\Http\Response
  31. */
  32. public function store(Request $request, Category $category, Group $group)
  33. {
  34. $request->validate($this->rules);
  35. $input = $request->all();
  36. $input["name"] = $input["name"] == null ? '' : $input["name"];
  37. $input["category_id"] = $category->id;
  38. if(request()->image)
  39. {
  40. $file = request()->image;
  41. $filename = time() . '_' . $file->getClientOriginalName();
  42. if (! File::exists(public_path()."/files/categories"))
  43. File::makeDirectory(public_path()."/files/categories");
  44. $path = public_path('files/categories');
  45. request()->image->move($path, $filename);
  46. $input["image"] = $filename;
  47. }
  48. Group::create($input);
  49. return redirect()->route('categories.groups.index', compact('category'))
  50. ->with('success','Category created successfully.');
  51. }
  52. /**
  53. * Display the specified resource.
  54. *
  55. * @param \App\Category $category
  56. * @return \Illuminate\Http\Response
  57. */
  58. public function show(Group $group, Category $category)
  59. {
  60. return view('groups.show',compact('category'));
  61. }
  62. /**
  63. * Show the form for editing the specified resource.
  64. *
  65. * @param \App\Category $category
  66. * @return \Illuminate\Http\Response
  67. */
  68. public function edit(Category $category, Group $group)
  69. {
  70. return view('groups.edit',compact('category', 'group'));
  71. }
  72. /**
  73. * Update the specified resource in storage.
  74. *
  75. * @param \Illuminate\Http\Request $request
  76. * @param \App\Category $category
  77. * @return \Illuminate\Http\Response
  78. */
  79. public function update(Request $request, Category $category, Group $group)
  80. {
  81. $request->validate($this->rules);
  82. $input = $request->all();
  83. $input["name"] = $input["name"] == null ? '' : $input["name"];
  84. if(request()->image)
  85. {
  86. $file = request()->image;
  87. $filename = time() . '_' . $file->getClientOriginalName();
  88. if (! File::exists(public_path()."/files/categories"))
  89. File::makeDirectory(public_path()."/files/categories");
  90. $path = public_path('files/categories');
  91. request()->image->move($path, $filename);
  92. $input["image"] = $filename;
  93. }
  94. $group->update($input);
  95. return redirect()->route('categories.groups.index', compact('category'))
  96. ->with('success','Category group updated successfully');
  97. }
  98. /**
  99. * Remove the specified resource from storage.
  100. *
  101. * @param \App\Category $category
  102. * @return \Illuminate\Http\Response
  103. */
  104. public function destroy(Category $category, Group $group)
  105. {
  106. $group->delete();
  107. return redirect()->route('categories.groups.index', compact('category'))
  108. ->with('success','Category group deleted successfully');
  109. }
  110. }