NoticeController.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Http\Requests\Admin\NoticeSave;
  4. use Illuminate\Http\Request;
  5. use App\Http\Controllers\Controller;
  6. use App\Models\Notice;
  7. use Illuminate\Support\Facades\Cache;
  8. class NoticeController extends Controller
  9. {
  10. public function fetch(Request $request)
  11. {
  12. return response([
  13. 'data' => Notice::orderBy('id', 'DESC')->get()
  14. ]);
  15. }
  16. public function save(NoticeSave $request)
  17. {
  18. $data = $request->only([
  19. 'title',
  20. 'content',
  21. 'img_url'
  22. ]);
  23. if (!$request->input('id')) {
  24. if (!Notice::create($data)) {
  25. abort(500, '保存失败');
  26. }
  27. } else {
  28. if (!Notice::find($request->input('id'))->update($data)) {
  29. abort(500, '保存失败');
  30. }
  31. }
  32. return response([
  33. 'data' => true
  34. ]);
  35. }
  36. public function drop(Request $request)
  37. {
  38. if (empty($request->input('id'))) {
  39. abort(500, '参数错误');
  40. }
  41. $notice = Notice::find($request->input('id'));
  42. if (!$notice) {
  43. abort(500, '公告不存在');
  44. }
  45. if (!$notice->delete()) {
  46. abort(500, '删除失败');
  47. }
  48. return response([
  49. 'data' => true
  50. ]);
  51. }
  52. }