|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Http\Controllers\Admin; |
| 4 | + |
| 5 | +use App\Article; |
| 6 | +use App\Category; |
| 7 | +use Illuminate\Http\Request; |
| 8 | +use App\Http\Controllers\Controller; |
| 9 | + |
| 10 | +class ArticleController extends Controller |
| 11 | +{ |
| 12 | + /** |
| 13 | + * Display a listing of the resource. |
| 14 | + * |
| 15 | + * @return \Illuminate\Http\Response |
| 16 | + */ |
| 17 | + public function index() |
| 18 | + { |
| 19 | + return view('admin.article.index', [ |
| 20 | + 'articles' => Article::orderBy('created_at', 'desc')->paginate(10) |
| 21 | + ]); |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * Show the form for creating a new resource. |
| 26 | + * |
| 27 | + * @return \Illuminate\Http\Response |
| 28 | + */ |
| 29 | + public function create() |
| 30 | + { |
| 31 | + return view('admin.article.create', [ |
| 32 | + 'article' => [], |
| 33 | + 'categories' => Category::with('children')->where('parent_id', 0)->get(), |
| 34 | + 'delimiter' => '' |
| 35 | + ]); |
| 36 | + } |
| 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 | + $article = Article::create($request->except(['categories', 'files'])); |
| 47 | + |
| 48 | + // Categories |
| 49 | + if($request->input('categories')) : |
| 50 | + $article->categories()->attach($request->input('categories')); |
| 51 | + endif; |
| 52 | + |
| 53 | + return redirect()->route('admin.article.index'); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Display the specified resource. |
| 58 | + * |
| 59 | + * @param \App\Article $article |
| 60 | + * @return \Illuminate\Http\Response |
| 61 | + */ |
| 62 | + public function show(Article $article) |
| 63 | + { |
| 64 | + // |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Show the form for editing the specified resource. |
| 69 | + * |
| 70 | + * @param \App\Article $article |
| 71 | + * @return \Illuminate\Http\Response |
| 72 | + */ |
| 73 | + public function edit(Article $article) |
| 74 | + { |
| 75 | + return view('admin.article.edit', [ |
| 76 | + 'article' => $article, |
| 77 | + 'categories' => Category::with('children')->where('parent_id', 0)->get(), |
| 78 | + 'delimiter' => '' |
| 79 | + ]); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Update the specified resource in storage. |
| 84 | + * |
| 85 | + * @param \Illuminate\Http\Request $request |
| 86 | + * @param \App\Article $article |
| 87 | + * @return \Illuminate\Http\Response |
| 88 | + */ |
| 89 | + public function update(Request $request, Article $article) |
| 90 | + { |
| 91 | + $article->update($request->except(['slug', 'categories', 'files'])); |
| 92 | + |
| 93 | + // Categories |
| 94 | + if ($request->has('title')) { |
| 95 | + $article->categories()->detach(); |
| 96 | + if($request->input('categories')) : |
| 97 | + $article->categories()->attach($request->input('categories')); |
| 98 | + endif; |
| 99 | + } |
| 100 | + |
| 101 | + return redirect()->route('admin.article.index'); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Remove the specified resource from storage. |
| 106 | + * |
| 107 | + * @param \App\Article $article |
| 108 | + * @return \Illuminate\Http\Response |
| 109 | + */ |
| 110 | + public function destroy(Article $article) |
| 111 | + { |
| 112 | + $article->categories()->detach(); |
| 113 | + $article->delete(); |
| 114 | + |
| 115 | + return redirect()->route('admin.article.index'); |
| 116 | + } |
| 117 | +} |
0 commit comments