CategoryController.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. $category = $this->category->getNestedList();
  25. return view("backend.category.index", compact('category'));
  26. }
  27. /**
  28. * Show the form for creating a new resource.
  29. *
  30. * @return \Illuminate\Http\Response
  31. */
  32. public function create()
  33. {
  34. return view('backend.category.create');
  35. }
  36. /**
  37. * Store a newly created resource in storage.
  38. *
  39. * @param \Illuminate\Http\Request $request
  40. * @return \Illuminate\Http\Response
  41. */
  42. public function store(CreateRequest $request)
  43. {
  44. $result = $this->category->store($request->all());
  45. if ($result) {
  46. return redirect('backend/category')->with('success', '分类添加成功');
  47. }
  48. return redirect(route('backend.category.create'))->withErrors('系统异常,分类添加失败');
  49. }
  50. /**
  51. * Display the specified resource.
  52. *
  53. * @param int $id
  54. * @return \Illuminate\Http\Response
  55. */
  56. public function show($id)
  57. {
  58. //
  59. }
  60. /**
  61. * Show the form for editing the specified resource.
  62. *
  63. * @param int $id
  64. * @return \Illuminate\Http\Response
  65. */
  66. public function edit($id)
  67. {
  68. $category = $this->category->find($id);
  69. return view('backend.category.edit', compact('category'));
  70. }
  71. /**
  72. * Update the specified resource in storage.
  73. *
  74. * @param \Illuminate\Http\Request $request
  75. * @param int $id
  76. * @return \Illuminate\Http\Response
  77. */
  78. public function update(UpdateRequest $request, $id)
  79. {
  80. $result = $this->category->update($request->all(), $id);
  81. if ($result) {
  82. return redirect('backend/category')->with('success', '分类修改成功');
  83. }
  84. return redirect(route('backend.category.edit', ['id' => $id]))->withErrors('系统异常,分类修改失败');
  85. }
  86. /**
  87. * Remove the specified resource from storage.
  88. *
  89. * @param int $id
  90. * @return \Illuminate\Http\Response
  91. */
  92. public function destroy($id)
  93. {
  94. $category = Category::find($id);
  95. if ($category) {
  96. if ($category->delete()) {
  97. return response()->json(['status' => 0]);
  98. }
  99. }
  100. return response()->json(['status' => 1]);
  101. }
  102. }