CategoryController.php 3.5 KB

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