CategoryController.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Models\Category;
  4. use Illuminate\Http\Request;
  5. use App\Http\Controllers\Controller;
  6. class CategoryController extends Controller
  7. {
  8. /**
  9. * Display a listing of the resource.
  10. *
  11. * @return \Illuminate\Http\Response
  12. */
  13. public function index()
  14. {
  15. return view('admin.category.index');
  16. }
  17. public function data(Request $request)
  18. {
  19. $res = Category::where('parent_id',$request->get('parent_id',0))->orderBy('id','desc')->orderBy('sort','desc')->paginate($request->get('limit',30))->toArray();
  20. $data = [
  21. 'code' => 0,
  22. 'msg' => '正在请求中...',
  23. 'count' => $res['total'],
  24. 'data' => $res['data']
  25. ];
  26. return response()->json($data);
  27. }
  28. /**
  29. * Show the form for creating a new resource.
  30. *
  31. * @return \Illuminate\Http\Response
  32. */
  33. public function create()
  34. {
  35. $categorys = $this->tree(Category::get()->toArray());
  36. return view('admin.category.create',compact('categorys'));
  37. }
  38. /**
  39. * Store a newly created resource in storage.
  40. *
  41. * @param \Illuminate\Http\Request $request
  42. * @return \Illuminate\Http\Response
  43. */
  44. public function store(Request $request)
  45. {
  46. $this->validate($request,[
  47. 'name' => 'required|string',
  48. 'sort' => 'required|numeric',
  49. 'parent_id' => 'required|numeric'
  50. ]);
  51. if (Category::create($request->all())){
  52. return redirect(route('admin.category'))->with(['status'=>'添加完成']);
  53. }
  54. return redirect(route('admin.category'))->with(['status'=>'系统错误']);
  55. }
  56. /**
  57. * Display the specified resource.
  58. *
  59. * @param int $id
  60. * @return \Illuminate\Http\Response
  61. */
  62. public function show($id)
  63. {
  64. //
  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::findOrFail($id);
  75. $categorys = $this->tree(Category::get()->toArray());
  76. return view('admin.category.edit',compact('category','categorys'));
  77. }
  78. /**
  79. * Update the specified resource in storage.
  80. *
  81. * @param \Illuminate\Http\Request $request
  82. * @param int $id
  83. * @return \Illuminate\Http\Response
  84. */
  85. public function update(Request $request, $id)
  86. {
  87. $this->validate($request,[
  88. 'name' => 'required|string',
  89. 'sort' => 'required|numeric',
  90. 'parent_id' => 'required|numeric'
  91. ]);
  92. $category = Category::findOrFail($id);
  93. if ($category->update($request->all())){
  94. return redirect(route('admin.category'))->with(['status'=>'更新成功']);
  95. }
  96. return redirect(route('admin.category'))->withErrors(['status'=>'系统错误']);
  97. }
  98. /**
  99. * Remove the specified resource from storage.
  100. *
  101. * @param int $id
  102. * @return \Illuminate\Http\Response
  103. */
  104. public function destroy(Request $request)
  105. {
  106. $ids = $request->get('ids');
  107. if (empty($ids)){
  108. return response()->json(['code'=>1,'msg'=>'请选择删除项']);
  109. }
  110. $category = Category::with(['childs','articles'])->find($ids);
  111. if (!$category){
  112. return response()->json(['code'=>1,'msg'=>'请选择删除项']);
  113. }
  114. if (!$category->childs->isEmpty() || !$category->articles->isEmpty()){
  115. return response()->json(['code'=>1,'msg'=>'该分类下有子分类或者文章,不能删除']);
  116. }
  117. if ($category->delete()){
  118. return response()->json(['code'=>0,'msg'=>'删除成功']);
  119. }
  120. return response()->json(['code'=>1,'msg'=>'删除失败']);
  121. }
  122. }