0

我正在创建一个调查应用程序,基本上当用户选择“选择”的输入类型时,它会出现一个可以动态增加的选项输入,但是我在我的数据库中插入这些选项时遇到了一些问题,我使用同步方法来在我的表中插入这些选项,但给我一个错误

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'toufarto.question_id' doesn't exist (SQL: select `option_question_id` from `question_id` where `id` = 11)

这是我的代码:

Tables:
questions:
- id;
- input_type;

option_question
- id;
- label_option;
- question_id

我的控制器:

 public function store(Request $request)
        {

$this->validate($request, array(
            'label_option'             => 'max:255',
            'input_type'            => 'required|integer'
        ));
            $question = new Question(); 
            $question->input_type = $request->input_type;
            $question->save();
            $question->options()->sync($request->option, false);

            Session::flash('success', 'success');

            return back();
        }

我的问题模型:

public function options()
    {

        return $this->belongsToMany(OptionQuestion::class,'question_id','id');
    }

我的选项问题模型:

class OptionQuestion extends Model
{
    protected $table = "option_question";
}

注意:我如何将标签列添加到“同步”方法,因为我需要从表单的选项字段中插入标签

4

1 回答 1

0

如 Laravel 文档中所述,您需要按如下方式定义参数:

$this->belongsToMany(Model,table_name);

或者

$this->belongsToMany(Model, table_name, table_1_key, table_2_key);

查看您的数据库原理图,但似乎您应该进行一些小的调整。

questions
 - id
 - input_type;

options
- id
- label_option

option_question
- id
- question_id
- option_id

问题模型

public function options()
{
    return $this->belongsToMany(OptionQuestion::class);
}

期权模型

public function questions()
{
    return $this->belongsToMany(Question::class);
}
于 2017-02-14T21:33:29.160 回答