ArticleController.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Http\Resources\Home\ArticleCollection;
  4. use App\Models\ArticleModel;
  5. use Illuminate\Http\Request;
  6. use App\Http\Controllers\Controller;
  7. class ArticleController extends Controller
  8. {
  9. //
  10. public function index(Request $request)
  11. {
  12. $articles = ArticleModel::when($request->filled('delete'), function ($query) {
  13. $query->where('deleted_at', 0);
  14. })->orderBy('id', 'desc')->paginate($request->get('per_page', 20));
  15. return new ArticleCollection($articles);
  16. }
  17. /**
  18. * 创建
  19. * @param Request $request
  20. */
  21. public function store(Request $request)
  22. {
  23. $this->validate($request, [
  24. 'title' => 'required|max:32',
  25. 'tag' => 'required|max:8',
  26. 'cover' => 'required|max:256',
  27. 'link' => 'required|max:256',
  28. 'showed_at' => 'required',
  29. ]);
  30. ArticleModel::create($request->all());
  31. return response([
  32. 'code' => 200,
  33. 'message' => 'OK'
  34. ]);
  35. }
  36. public function update(Request $request, $id)
  37. {
  38. $this->validate($request, [
  39. 'title' => 'max:32',
  40. 'tag' => 'max:8',
  41. 'cover' => 'max:256',
  42. 'link' => 'max:256',
  43. 'showed_at' => '',
  44. ]);
  45. $article = ArticleModel::find($id);
  46. $article->update($request->all());
  47. return response([
  48. 'code' => 200,
  49. 'message' => 'OK'
  50. ]);
  51. }
  52. /**
  53. * 删除
  54. * @param Request $request
  55. * @param $id
  56. * @return \Illuminate\Http\JsonResponse
  57. * @throws \Exception
  58. */
  59. public function destroy(Request $request, $id)
  60. {
  61. $article = ArticleModel::find($id);
  62. $article->delete();
  63. return response([
  64. 'code' => 200,
  65. 'message' => 'OK'
  66. ]);
  67. }
  68. }