validate($request, [ 'type' => 'required|in:text,music', 'question' => 'required', 'tag_id' => 'integer', 'author' => 'max:8' ]); $attribute = $request->only('type', 'question', 'tag_id', 'author'); switch ($request->post('type', 'text')) { case "music": $attribute['question'] = json_encode($request->post('question'), JSON_UNESCAPED_UNICODE); break; } QuestionTemplateModel::create($attribute); return response([ 'code' => 200, 'message' => 'OK' ]); } // 修改问题 public function update(Request $request, $id) { $this->validate($request, [ 'type' => 'in:text,music', 'question' => '', 'tag_id' => 'integer', 'author' => 'max:8', 'hide' => '', ]); $attribute = $request->only('type', 'question', 'tag_id', 'author'); switch ($request->post('type', 'text')) { case "music": $attribute['question'] = json_encode($request->post('question'), JSON_UNESCAPED_UNICODE); break; } $question = QuestionTemplateModel::findOrFail($id); $question->update($attribute); return response([ 'code' => 200, 'message' => 'OK' ]); } // 标签列表 public function index(Request $request) { $tags = QuestionTagModel::where('type', $request->get('type', 'text'))->get(); foreach ($tags as $tag) { $questions = QuestionTemplateModel::where('tag_id', $tag->id)->get(); foreach ($questions as $question) { switch ($question->type) { case "music": $question->question = json_decode($question->question, true); break; } } $tag->questions = $questions; } return new QuestionTagCollection($tags); } // 创建标签 public function createTag(Request $request) { $this->validate($request, [ 'type' => 'required|in:text,music', 'tag' => 'required|max:8' ]); QuestionTagModel::create($request->all()); return response([ 'code' => 200, 'message' => 'OK' ]); } // 修改标签 public function updateTag(Request $request, $id) { $this->validate($request, [ 'type' => 'in:text,music', 'tag' => 'max:8' ]); $questionTag = QuestionTagModel::findOrFail($id); $questionTag->update($request->all()); return response([ 'code' => 200, 'message' => 'OK' ]); } }