ArticleController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Http\Requests\ArticleRequest;
  4. use App\Models\Article;
  5. use App\Models\Category;
  6. use App\Models\Tag;
  7. use Illuminate\Http\Request;
  8. use App\Http\Controllers\Controller;
  9. class ArticleController extends Controller
  10. {
  11. /**
  12. * Display a listing of the resource.
  13. *
  14. * @return \Illuminate\Http\Response
  15. */
  16. public function index()
  17. {
  18. //分类
  19. $categorys = Category::with('allChilds')->where('parent_id',0)->orderBy('sort','desc')->get();
  20. return view('admin.article.index',compact('categorys'));
  21. }
  22. public function data(Request $request)
  23. {
  24. $model = Article::query();
  25. if ($request->get('category_id')){
  26. $model = $model->where('category_id',$request->get('category_id'));
  27. }
  28. if ($request->get('title')){
  29. $model = $model->where('title','like','%'.$request->get('title').'%');
  30. }
  31. $res = $model->orderBy('created_at','desc')->with(['tags','category'])->paginate($request->get('limit',30))->toArray();
  32. $data = [
  33. 'code' => 0,
  34. 'msg' => '正在请求中...',
  35. 'count' => $res['total'],
  36. 'data' => $res['data']
  37. ];
  38. return response()->json($data);
  39. }
  40. /**
  41. * Show the form for creating a new resource.
  42. *
  43. * @return \Illuminate\Http\Response
  44. */
  45. public function create()
  46. {
  47. //分类
  48. $categorys = Category::with('allChilds')->where('parent_id',0)->orderBy('sort','desc')->get();
  49. //标签
  50. $tags = Tag::get();
  51. return view('admin.article.create',compact('tags','categorys'));
  52. }
  53. /**
  54. * Store a newly created resource in storage.
  55. *
  56. * @param \Illuminate\Http\Request $request
  57. * @return \Illuminate\Http\Response
  58. */
  59. public function store(ArticleRequest $request)
  60. {
  61. $data = $request->only(['category_id','title','keywords','description','content','thumb','click']);
  62. $article = Article::create($data);
  63. if ($article && !empty($request->get('tags')) ){
  64. $article->tags()->sync($request->get('tags'));
  65. }
  66. return redirect(route('admin.article'))->with(['status'=>'添加成功']);
  67. }
  68. /**
  69. * Display the specified resource.
  70. *
  71. * @param int $id
  72. * @return \Illuminate\Http\Response
  73. */
  74. public function show($id)
  75. {
  76. //
  77. }
  78. /**
  79. * Show the form for editing the specified resource.
  80. *
  81. * @param int $id
  82. * @return \Illuminate\Http\Response
  83. */
  84. public function edit($id)
  85. {
  86. $article = Article::with('tags')->findOrFail($id);
  87. if (!$article){
  88. return redirect(route('admin.article'))->withErrors(['status'=>'文章不存在']);
  89. }
  90. //分类
  91. $categorys = Category::with('allChilds')->where('parent_id',0)->orderBy('sort','desc')->get();
  92. //标签
  93. $tags = Tag::get();
  94. foreach ($tags as $tag){
  95. $tag->checked = $article->tags->contains($tag) ? 'checked' : '';
  96. }
  97. return view('admin.article.edit',compact('article','categorys','tags'));
  98. }
  99. /**
  100. * Update the specified resource in storage.
  101. *
  102. * @param \Illuminate\Http\Request $request
  103. * @param int $id
  104. * @return \Illuminate\Http\Response
  105. */
  106. public function update(ArticleRequest $request, $id)
  107. {
  108. $article = Article::with('tags')->findOrFail($id);
  109. $data = $request->only(['category_id','title','keywords','description','content','thumb','click']);
  110. if ($article->update($data)){
  111. $article->tags()->sync($request->get('tags',[]));
  112. return redirect(route('admin.article'))->with(['status'=>'更新成功']);
  113. }
  114. return redirect(route('admin.article'))->withErrors(['status'=>'系统错误']);
  115. }
  116. /**
  117. * Remove the specified resource from storage.
  118. *
  119. * @param int $id
  120. * @return \Illuminate\Http\Response
  121. */
  122. public function destroy(Request $request)
  123. {
  124. $ids = $request->get('ids');
  125. if (empty($ids)){
  126. return response()->json(['code'=>1,'msg'=>'请选择删除项']);
  127. }
  128. foreach (Article::whereIn('id',$ids)->get() as $model){
  129. //清除中间表数据
  130. $model->tags()->sync([]);
  131. //删除文章
  132. $model->delete();
  133. }
  134. return response()->json(['code'=>0,'msg'=>'删除成功']);
  135. }
  136. }