NoticeController.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace App\Http\Controllers\Staff;
  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. try {
  29. Notice::find($request->input('id'))->update($data);
  30. } catch (\Exception $e) {
  31. abort(500, '保存失败');
  32. }
  33. }
  34. return response([
  35. 'data' => true
  36. ]);
  37. }
  38. public function drop(Request $request)
  39. {
  40. if (empty($request->input('id'))) {
  41. abort(500, '参数错误');
  42. }
  43. $notice = Notice::find($request->input('id'));
  44. if (!$notice) {
  45. abort(500, '公告不存在');
  46. }
  47. if (!$notice->delete()) {
  48. abort(500, '删除失败');
  49. }
  50. return response([
  51. 'data' => true
  52. ]);
  53. }
  54. }