CategoryController.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Models\Category;
  4. use Illuminate\Http\Request;
  5. use App\Http\Requests;
  6. use Illuminate\Support\Facades\Artisan;
  7. class CategoryController extends AdminController
  8. {
  9. /*权限验证规则*/
  10. protected $validateRules = [
  11. 'name' => 'required|max:255',
  12. 'slug' => 'required|max:255|unique:categories',
  13. 'sort' => 'required|integer'
  14. ];
  15. /**
  16. * 分类列表页面
  17. *
  18. * @return \Illuminate\Http\Response
  19. */
  20. public function index(Request $request)
  21. {
  22. $parentId = $request->input("parent_id",0);
  23. $parentCategoies = Category::getParentCategories($parentId);
  24. $categories = Category::where("parent_id","=",$parentId)->orderBy('sort','asc')->orderBy('created_at','asc')->paginate(config('tipask.admin.page_size'));
  25. return view("admin.category.index")->with(compact('categories','parentCategoies','parentId'));
  26. }
  27. /**
  28. * Show the form for creating a new resource.
  29. *
  30. * @return \Illuminate\Http\Response
  31. */
  32. public function create(Request $request)
  33. {
  34. $parentId = $request->input("parent_id",0);
  35. $parentCategory['id'] = $parentId ;
  36. $parentCategory['name'] = '无' ;
  37. if($parentId){
  38. $category = Category::findOrFail($parentId);
  39. $parentCategory['name'] = $category->name;
  40. }
  41. return view('admin.category.create')->with(compact('parentCategory'));
  42. }
  43. /**
  44. * Store a newly created resource in storage.
  45. *
  46. * @param \Illuminate\Http\Request $request
  47. * @return \Illuminate\Http\Response
  48. */
  49. public function store(Request $request)
  50. {
  51. $request->flash();
  52. $this->validate($request,$this->validateRules);
  53. $parentId = $request->input("parent_id",0);
  54. $types = $request->input("types",[]);
  55. $formData = $request->all();
  56. $formData['type'] = implode(",",$types);
  57. $formData['grade'] = 1;
  58. if($parentId){
  59. $parentCategory = Category::findOrFail($parentId);
  60. $formData['grade'] = $parentCategory->grade + 1;
  61. }
  62. Category::create($formData);
  63. Artisan::call('cache:clear');
  64. return $this->success(route('admin.category.index',['parent_id'=>$parentId]),'分类添加成功');
  65. }
  66. /**
  67. * Show the form for editing the specified resource.
  68. *
  69. * @param int $id
  70. * @return \Illuminate\Http\Response
  71. */
  72. public function edit($id)
  73. {
  74. $category = Category::find($id);
  75. if(!$category){
  76. return $this->error(route('admin.category.index'),'分类不存在,请核实');
  77. }
  78. return view('admin.category.edit')->with(compact('category'));
  79. }
  80. /**
  81. * Update the specified resource in storage.
  82. *
  83. * @param \Illuminate\Http\Request $request
  84. * @param int $id
  85. * @return \Illuminate\Http\Response
  86. */
  87. public function update(Request $request, $id)
  88. {
  89. $category = Category::find($id);
  90. if(!$category){
  91. return $this->error(route('admin.category.index'),'分类不存在,请核实');
  92. }
  93. $this->validateRules['slug'] = "required|max:255|unique:categories,slug,".$category->id;
  94. $this->validate($request,$this->validateRules);
  95. $category->name = $request->input('name');
  96. $category->slug = $request->input('slug');
  97. $category->sort = $request->input('sort');
  98. $category->status = $request->input('status');
  99. $category->type = implode(",",$request->input('types'));
  100. $category->save();
  101. Artisan::call('cache:clear');
  102. return $this->success(route('admin.category.index',['parent_id'=>$category->parent_id]),'分类添加成功');
  103. }
  104. /**
  105. * Remove the specified resource from storage.
  106. *
  107. * @param int $id
  108. * @return \Illuminate\Http\Response
  109. */
  110. public function destroy(Request $request)
  111. {
  112. Category::destroy($request->input('ids'));
  113. Artisan::call('cache:clear');
  114. return $this->success(route('admin.category.index'),'分类删除成功');
  115. }
  116. }