2016-11-23 104 views
0

我正在创建一个应用程序,该应用程序中有一个简单的多项选择测验功能。所以我有一个Question实体和一个Answer实体。我定义了2个关系,一对多(问题可以有很多答案)和一对一(问题可以有一个正确的答案)。以下是我迄今为止:laravel保存一对一和一对多的关系

问题型号:

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; 

问题是我的问题实体没有保存正确答案。所有的答案都在应答表中妥善保存,但我只需要做到这一点,所以我知道哪个是正确的。

任何人都可以看到我做错了什么?

在此先感谢!

+0

你的控制器是一个部件,你会显示完整的功能。 –

+0

@KrisRoofe我加了剩下的部分,但我不认为它会有所帮助,它只是从请求中获取输入。 – Sehael

回答

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的专家,所以我不知道为什么有差别。

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,你需要指定要使用的自定义键。

+0

谢谢,但没有奏效。另外,我相信只要我有在我的模型中定义的'$ primaryKey',我应该没问题。 – Sehael

+0

事实证明,我确实需要定义其中一个关系的列。但我不需要为所有关系做这件事,而且它是我需要在我的'Question'实体中定义的'answer_id'。谢谢! – Sehael