GroupController.php 3.9 KB

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