我正在创建一个具有简单多项选择测验功能的应用程序。所以我有一个Question
实体和一个Answer
实体。我有两个关系定义,一对多(问题可以有很多答案)和一对一(问题可以有一个正确的答案)。以下是我到目前为止的情况:
问题模型:
class Question extends Model
{
public $table = 'question';
public $primaryKey = 'question_id';
public $fillable = ['question', 'answer_id'];
public function answers()
{
return $this->hasMany('App\Models\Answer');
}
public function correctanswer()
{
return $this->hasOne('App\Models\Answer');
}
}
答案型号:
class Answer extends Model
{
public $table = 'answer';
public $primaryKey = 'answer_id';
public $guarded = ['created_at', 'updated_at'];
public function question()
{
return $this->belongsTo('App\Models\Question');
}
public function correctquestion()
{
return $this->belongsTo('App\Models\Question');
}
}
在我的控制器中:
$input = $request->all();
$answers = $input['answer'];
unset($input['answer']);
$question = Question::create($input);
foreach($answers as $key=>$a){
$arr['question_id'] = $question->question_id;
$arr['answer'] = $a;
$answer = new Answer($arr);
$question->answers()->save($answer); //save all answers - this works
if($key == 0){ //the first submitted answer is the correct one
$question->correctanswer()->save($answer); //save correct answer - this doesn't work
}
}
我的db表定义:
CREATE TABLE IF NOT EXISTS question (
question_id int(11) unsigned NOT NULL AUTO_INCREMENT,
question text,
created_at timestamp,
updated_at timestamp,
answer_id int(11) unsigned,
PRIMARY KEY (question_id),
FOREIGN KEY (answer_id)
REFERENCES answer(answer_id)
ON DELETE NO ACTION
ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE IF NOT EXISTS answer (
answer_id int(11) unsigned NOT NULL AUTO_INCREMENT,
answer text,
created_at timestamp,
updated_at timestamp,
question_id int(11) unsigned,
PRIMARY KEY (answer_id),
FOREIGN KEY (question_id)
REFERENCES question(question_id)
ON DELETE CASCADE
ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
问题是我的问题实体没有保存正确的答案。所有答案都在Answer表中正确保存,但我只需要这样做就知道哪个是正确的。
谁能看到我做错了什么?
提前致谢!
答案 0 :(得分:1)
尝试更改关键字定义,
class Question extends Model
{
public function answers()
{
return $this->hasMany('App\Models\Answer', 'question_id');
}
public function correctanswer()
{
return $this->hasOne('App\Models\Answer', 'question_id');
}
}
class Answer extends Model
{
public function question()
{
return $this->belongsTo('App\Models\Question', 'question_id');
}
public function correctquestion()
{
return $this->belongsTo('App\Models\Question', 'question_id');
}
}
由于您的主键未命名为id
,因此您需要指定正在使用的自定义键。
答案 1 :(得分:1)
事实证明,这是一个实体拥有这种关系的问题。这是我改变它使它工作:
在我的问题模型中:
public function correctanswer()
{
return $this->hasOne('App\Models\Answer');
}
应该是
public function correctanswer()
{
return $this->belongsTo('App\Models\Answer', 'answer_id', 'answer_id');
}
在我的答案模型中:
public function correctquestion()
{
return $this->belongsTo('App\Models\Question');
}
应该是:
public function correctquestion()
{
return $this->hasOne('App\Models\Question');
}
然后在我的控制器中,我改变了我保存正确答案的方式:
$question = Question::create($input);
foreach($answers as $key=>$a){
$arr['question_id'] = $question->question_id;
$arr['answer'] = $a;
$answer = new Answer($arr);
$question->answers()->save($answer);
if($key == 0){
//$question->correctanswer()->save($answer);
$answer->correctquestion()->save($question);
}
}
我还不是Laravel的专家,所以我不确定为什么会有所作为。