1
0

CategoryController.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace App\Http\Controllers\Backend;
  3. use App\Repositories\CategoryRepositoryEloquent;
  4. use Illuminate\Http\Request;
  5. use App\Http\Requests;
  6. use App\Http\Controllers\Controller;
  7. use App\Models\Category;
  8. use App\Http\Requests\Backend\Category\CreateRequest;
  9. use App\Http\Requests\Backend\Category\UpdateRequest;
  10. class CategoryController extends Controller
  11. {
  12. protected $category;
  13. public function __construct(CategoryRepositoryEloquent $category)
  14. {
  15. $this->category = $category;
  16. }
  17. /**
  18. * Display a listing of the resource.
  19. *
  20. * @return \Illuminate\Http\Response
  21. */
  22. public function index()
  23. {
  24. /*$root = Category::create(['name' => 'Root category2']);
  25. $child1 = $root->children()->create(['name' => 'Child 1']);
  26. // with the `makeChildOf` method
  27. $child2 = Category::create(['name' => 'Child 2']);
  28. $child2->makeChildOf($root);
  29. exit();*/
  30. $category = $this->category->getNestedList();
  31. return view("backend.category.index", compact('category'));
  32. }
  33. /**
  34. * Show the form for creating a new resource.
  35. *
  36. * @return \Illuminate\Http\Response
  37. */
  38. public function create()
  39. {
  40. return view('backend.category.create');
  41. }
  42. /**
  43. * Store a newly created resource in storage.
  44. *
  45. * @param \Illuminate\Http\Request $request
  46. * @return \Illuminate\Http\Response
  47. */
  48. public function store(CreateRequest $request)
  49. {
  50. $result = $this->category->store($request->all());
  51. if ($result) {
  52. return redirect('backend/category')->with('success', '分类添加成功');
  53. }
  54. return redirect(route('backend.category.create'))->withErrors('系统异常,分类添加失败');
  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 = $this->category->find($id);
  75. return view('backend.category.edit', compact('category'));
  76. }
  77. /**
  78. * Update the specified resource in storage.
  79. *
  80. * @param \Illuminate\Http\Request $request
  81. * @param int $id
  82. * @return \Illuminate\Http\Response
  83. */
  84. public function update(UpdateRequest $request, $id)
  85. {
  86. $result = $this->category->update($request->all(), $id);
  87. if ($result) {
  88. return redirect('backend/category')->with('success', '分类修改成功');
  89. }
  90. return redirect(route('backend.category.edit', ['id' => $id]))->withErrors('系统异常,分类修改失败');
  91. }
  92. /**
  93. * Remove the specified resource from storage.
  94. *
  95. * @param int $id
  96. * @return \Illuminate\Http\Response
  97. */
  98. public function destroy($id)
  99. {
  100. $category = Category::find($id);
  101. if ($category) {
  102. if ($category->delete()) {
  103. return response()->json(['status' => 0]);
  104. }
  105. }
  106. return response()->json(['status' => 1]);
  107. }
  108. }