NoticeController.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 Cache;
  8. class NoticeController extends Controller
  9. {
  10. public function fetch (Request $request) {
  11. return response([
  12. 'data' => Notice::orderBy('id', 'DESC')->get()
  13. ]);
  14. }
  15. public function save (NoticeSave $request) {
  16. $data = $request->only([
  17. 'title',
  18. 'content',
  19. 'img_url'
  20. ]);
  21. if (!$request->input('id')) {
  22. if (!Notice::create($data)) {
  23. abort(500, '保存失败');
  24. }
  25. } else {
  26. if (!Notice::find($request->input('id'))->update($data)) {
  27. abort(500, '保存失败');
  28. }
  29. }
  30. return response([
  31. 'data' => true
  32. ]);
  33. }
  34. public function drop (Request $request) {
  35. if (empty($request->input('id'))) {
  36. abort(500, '参数错误');
  37. }
  38. $notice = Notice::find($request->input('id'));
  39. if (!$notice) {
  40. abort(500, '公告不存在');
  41. }
  42. if (!$notice->delete()) {
  43. abort(500, '删除失败');
  44. }
  45. return response([
  46. 'data' => true
  47. ]);
  48. }
  49. }