NoticeController.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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\Redis;
  8. class NoticeController extends Controller
  9. {
  10. public function index (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 (!Notice::create($data)) {
  22. abort(500, '保存失败');
  23. }
  24. return response([
  25. 'data' => true
  26. ]);
  27. }
  28. public function update (NoticeSave $request) {
  29. $data = $request->only([
  30. 'title',
  31. 'content',
  32. 'img_url'
  33. ]);
  34. if (!Notice::where('id', $request->input('id'))->update($data)) {
  35. abort(500, '保存失败');
  36. }
  37. return response([
  38. 'data' => true
  39. ]);
  40. }
  41. public function drop (Request $request) {
  42. if (empty($request->input('id'))) {
  43. abort(500, '参数错误');
  44. }
  45. $notice = Notice::find($request->input('id'));
  46. if (!$notice) {
  47. abort(500, '公告不存在');
  48. }
  49. if (!$notice->delete()) {
  50. abort(500, '删除失败');
  51. }
  52. return response([
  53. 'data' => true
  54. ]);
  55. }
  56. }