php laravel 判断是否存在

小轩1986 发布于 2016/07/06 15:31
阅读 490
收藏 0

 我想问一下怎么判断数据库里要保存question_id是否存在,如果存在则替换原先的question_answer 存入数据库,如果不存在直接存入数据库

 public function postCollect(Request $request)

    {
            $question= new Question;
            $question_list->uesr_id = Auth::id();
            $question_list->question_id = Input::get('question_id')
            $question_list->question_answer = Input::get('question_answer');

            $question_list->save();

              return "success";
        }

加载中
1
huison
huison
public function postCollect(Request $request) {

	$question_id = $request->input('question_id');

	$question = Question::where('question_id', $question_id)->first();

	if (!$question) {
		$question = new Question;
	}

	$question->uesr_id = Auth::id();
	$question->question_id = $question_id;
	$question->question_answer = $request->input('question_answer');
	$question->save();

	return "success";
}



小轩1986
小轩1986
谢谢
返回顶部
顶部