为什么不将其保存到我的数据库中?尝试保存到两个表中

时间:2019-10-10 17:41:04

标签: database laravel vue.js

我正在尝试保存一个带有选项的问题,这些选项被保存到该问题本身以外的另一个表中。

这是我的控制器

public function store(Request $request)
{
    DB::beginTransaction();
    $preg = new SurveyQuestion;
    $preg->question = $request->question;
    $preg->survey_section_id = $request->survey_section_id;
    $preg->response_type_id = $request->response_type_id;
    $preg->optional = $request->optional;
    $preg->save();

    if ($request->get('questionOptions')) {
        $options = [];
        $preg_id = $preg->id;

        foreach ($request->get('questionOptions') as $item) {
            $option = $item['option'];
            if (isset($survey_question_id, $option)) {
                $options[] = [
                  "survey_question_id" => $preg_id,
                  "option" => $option
                ];
            }
        }
        if (count($options)) {
            SurveyQuestionOption::insert($options);
        }
    }

    DB::commit();
    return back();
}

这些是表格

Schema::create('survey_questions', function (Blueprint $table) {
    $table->increments('id');
    $table->string('question');
    $table->integer('survey_section_id')->unsigned();
    $table->integer('response_type_id')->unsigned();
    $table->boolean('optional');
    $table->timestamps();
    $table->foreign('survey_section_id')->references('id')->on('survey_sections');
    $table->foreign('response_type_id')->references('id')->on('response_types');
});

Schema::create('survey_question_options', function (Blueprint $table) {
    $table->increments('id');
    $table->string('option');
    $table->integer('survey_question_id')->unsigned();
    $table->timestamps();
    $table->foreign('survey_question_id')->references('id')->on('survey_questions');
});

这样的关系

SurveyQuestion         ->   hasMany     ->   SurveyQuestionOption
SurveyQuestionOption   ->   belongsTo   ->   SurveyQuestion

在我的Vue中,我将questionOptions作为内部对象的数组,并进行了这样的设置,因为我需要选项的问题最少为两个,并以类似Submit方法的方式发送跟随

questionOptions: [
    {
        option: ''
    },
    {
       option: ''
    },
],
submit() {
    this.$refs.form.validate((valid) => {
        if (valid) {
            this.loading = true;
            this.$inertia.post(this.baseUrl, {
                question: this.form.question,
                survey_section_id: this.form.survey_section_id,
                response_type_id: this.form.response_type_id,
                optional: this.form.optional,
                questionOptions: this.form.questionOptions
            }).then(
                () => {
                    this.survey_question = this.$page.survey_question;
                    this.$message({
                       type: 'success',
                       message: 'Creado correctamente.'
                    });
                    this.loading = false
                },
                (res) => {
                    this.$message.error(parseError(res)[0]);
                    this.loading = false;
                })
            } 
        } else {
            return false;
        }
        this.reset();
    });
},                        

之所以这样做,是因为某些问题没有这些选项,只有大约1/4的选项具有这些选项,这取决于所选的响应类型。但是它没有保存。任何帮助表示赞赏。

0 个答案:

没有答案