bannerModel = new BannerModel(); } /** * 添加广告 * @param Request $request * @return array */ public function store(Request $request) { $banner = $this->bannerModel->fill($request->toArray()); if ($banner->save()) { return array( 'code' => 200, 'message' => 'success', 'data' => [ 'banner_id' => $banner->id ], ); } else { return array( 'code' => 505, 'message' => '数据库异常' ); } } /** * 更新广告 * @param Request $request * @param int $banner_id * @return array */ public function update(Request $request, int $banner_id) { $banner = $this->bannerModel->find($banner_id); if (collect($banner)->isEmpty()) { return array( 'code' => 101, 'message' => '参数错误' ); } $banner->fill($request->toArray()); if ($banner->save()) { return array( 'code' => 200, 'message' => 'success' ); } else { return array( 'code' => 505, 'message' => '数据库异常' ); } } /** * 删除广告 * @param int $banner_id * @return array * @throws \Exception */ public function delete(int $banner_id) { $banner = $this->bannerModel->find($banner_id); if (collect($banner)->isEmpty()) { return array( 'code' => 101, 'message' => '参数错误' ); } if ($banner->delete()) { return array( 'code' => 200, 'message' => 'success' ); } else { return array( 'code' => 505, 'message' => '数据库异常' ); } } /** * 获取广告信息 * @param int $banner_id * @return array */ public function get(int $banner_id) { $banner = $this->bannerModel->find($banner_id); if (collect($banner)->isEmpty()) { return array( 'code' => 101, 'message' => '参数错误' ); } return array( 'code' => 200, 'message' => 'success', 'data' => $banner ); } /** * 获取某位置的广告 * @param $position * @return array */ public function listByPosition($position) { $banners = $this->bannerModel->where('position', $position)->orderBy('sort', 'desc')->get(); return array( 'code' => 200, 'message' => 'success', 'data' => $banners ); } }