CategoryController.php 3.7 KB

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