get('city_id'); $keyword = $request->get('keyword'); $limit = $request->get('limit', 10); $listQuery = Qas::query(); if (!empty($city_id)) { $listQuery->where(['status' => 1, 'city_id' => $city_id]); } if (!empty($keyword)) { $qaIds = QaQuestions::whereRaw("title LIKE ? OR content LIKE ?", ['%'.$keyword.'%', '%'.$keyword.'%']) ->column('qa_id'); $listQuery->when(!empty($qaIds), function ($query) use ($qaIds) { $query->whereIn('id', $qaIds); // 请将 'qa_id_field' 替换为实际的关联字段名 }); } $list = $listQuery->order('id', 'desc') ->with(['qaQuestions' => function ($query) { $query->order('sort', 'desc'); }]) ->paginate($limit); return $this->success($list); } /** * cms list * @param Request $request * @return \support\Response * @throws \think\db\exception\DbException */ public function getQa(Request $request) { $title = $request->get('title'); $status = $request->get('status'); Log::warning('======',['aa'=>$status]); $list = Qas::with(['qaCitys','qaQuestions'=>function ($query) { $query->order('sort desc'); }])->order('create_time desc'); if (!empty($title)){ $list = $list->where('title','like','%'.$title.'%'); } if (!is_null($status)){ $list = $list->where('status',$status); } $list = $list->paginate($request->get('limit',10)); return $this->success($list); } public function getQaDetail(Request $request) { $id = $request->get('id'); if (empty($id)) return $this->error(2001, 'id data cannot be empty!'); $data = Qas::with(['qaCitys','qaQuestions'=>function ($query) { $query->order('sort asc'); }])->where('status',1)->find(); return $this->success($data); } public function addQa(Request $request) { $post = $request->post(); if (empty($post['city_id'])) return $this->error(2001, 'city_id data cannot be empty!'); if (empty($post['title'])) return $this->error(2001, 'title data cannot be empty!'); $qaQuestion = $post['qaQuestions'] ?? null; unset($post['qaQuestions']); try { Db::transaction(function () use ($post,$qaQuestion){ $data = Qas::create($post); if (!empty($qaQuestion)){ foreach ($qaQuestion as $k => $v){ $qaQuestion = new QaQuestions(); $qaQuestion->qa_id = $data->id; $qaQuestion->title = $v['title']; $qaQuestion->content = $v['content']; $qaQuestion->sort = $v['sort']; $qaQuestion->save(); } $data->qaQuestions = $qaQuestion; } }); } catch (\Exception $e) { return $this->error(2002, $e->getMessage()); } return $this->success(null); } public function editQa(Request $request) { $post = $request->post(); if (empty($post['id'])) return $this->error(2001, 'id data cannot be empty!'); $qaQuestion = $post['qaQuestions'] ?? null; unset($post['create_time']); unset($post['update_time']); unset($post['qaQuestions']); try { Db::transaction(function () use ($post,$qaQuestion){ $data = Qas::update($post); if (!empty($qaQuestion)){ QaQuestions::where('qa_id',$post['id'])->delete(); foreach ($qaQuestion as $k => $v){ $qaQuestion = new QaQuestions(); $qaQuestion->qa_id = $data->id; $qaQuestion->title = $v['title']; $qaQuestion->content = $v['content']; $qaQuestion->sort = $v['sort']; $qaQuestion->save(); } $data->qa_question = $qaQuestion; } }); } catch (\Exception $e) { return $this->error(2002, $e->getMessage()); } return $this->success(null); } public function delQa(Request $request) { $id = $request->post('id'); if (empty($id)) return $this->error(2001, 'id data cannot be empty!'); try { Db::transaction(function () use ($id){ Qas::destroy($id); QaQuestions::where('qa_id',$id)->delete(); }); } catch (\Exception $e) { return $this->error(2002, $e->getMessage()); } return $this->success(null); } public function getQaCityList() { $list = QaCitys::order('sort desc')->select(); return $this->success($list); } }