BannerController.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\Models\BannerModel;
  5. class BannerController extends Controller
  6. {
  7. private $bannerModel;
  8. public function __construct()
  9. {
  10. $this->bannerModel = new BannerModel();
  11. }
  12. /**
  13. * 添加广告
  14. * @param Request $request
  15. * @return array
  16. */
  17. public function store(Request $request)
  18. {
  19. $banner = $this->bannerModel->create($request->all());
  20. return response([
  21. 'code' => 200,
  22. 'message' => 'success',
  23. 'data' => [
  24. 'banner_id' => $banner->id
  25. ]
  26. ]);
  27. }
  28. /**
  29. * 更新广告
  30. * @param Request $request
  31. * @param int $banner_id
  32. * @return array
  33. */
  34. public function update(Request $request, int $banner_id)
  35. {
  36. $banner = $this->bannerModel->findOrFail($banner_id);
  37. $banner->fill($request->all());
  38. $banner->save();
  39. return response([
  40. 'code' => 200,
  41. 'message' => 'success'
  42. ]);
  43. }
  44. /**
  45. * 删除广告
  46. * @param int $banner_id
  47. * @return array
  48. * @throws \Exception
  49. */
  50. public function delete(int $banner_id)
  51. {
  52. $banner = $this->bannerModel->findOrFail($banner_id);
  53. $banner->delete();
  54. return response([
  55. 'code' => 200,
  56. 'message' => 'success'
  57. ]);
  58. }
  59. /**
  60. * 获取广告信息
  61. * @param int $banner_id
  62. * @return array
  63. */
  64. public function get(int $banner_id)
  65. {
  66. $banner = $this->bannerModel->findOrFail($banner_id);
  67. return response([
  68. 'code' => 200,
  69. 'message' => 'success',
  70. 'data' => $banner
  71. ]);
  72. }
  73. /**
  74. * 获取某位置的广告
  75. * @param $position
  76. * @return array
  77. */
  78. public function listByPosition($position)
  79. {
  80. $banners = $this->bannerModel->where('position', $position)
  81. ->orderBy('sort', 'desc')
  82. ->get();
  83. return response([
  84. 'code' => 200,
  85. 'message' => 'success',
  86. 'data' => $banners
  87. ]);
  88. }
  89. }