laravel 保存一对一和一对多关系
laravel save one to one and one to many relationship
我正在创建一个应用程序,其中包含一个简单的多项选择测验功能。所以我有一个 Question
实体和一个 Answer
实体。我定义了 2 种关系,一对多(问题可以有很多答案)和一对一(问题可以有一个正确答案)。这是我目前所拥有的:
问题模型:
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
}
}
我的数据库 table 定义:
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;
问题是我的问题实体没有保存正确答案。所有答案都正确保存在答案 table 中,但我只需要保存它,以便我知道哪个是正确的。
谁能看出我做错了什么?
提前致谢!
尝试像这样更改关系定义,
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
,您需要指定您正在使用的自定义键。
原来是哪个实体拥有关系的问题。这是我为使它起作用所做的更改:
在我的问题模型中:
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 方面的专家,所以我不确定为什么会有所不同。
我正在创建一个应用程序,其中包含一个简单的多项选择测验功能。所以我有一个 Question
实体和一个 Answer
实体。我定义了 2 种关系,一对多(问题可以有很多答案)和一对一(问题可以有一个正确答案)。这是我目前所拥有的:
问题模型:
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
}
}
我的数据库 table 定义:
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;
问题是我的问题实体没有保存正确答案。所有答案都正确保存在答案 table 中,但我只需要保存它,以便我知道哪个是正确的。
谁能看出我做错了什么?
提前致谢!
尝试像这样更改关系定义,
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
,您需要指定您正在使用的自定义键。
原来是哪个实体拥有关系的问题。这是我为使它起作用所做的更改:
在我的问题模型中:
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 方面的专家,所以我不确定为什么会有所不同。