我有一个问题,我需要复制所选的调查。当我单击按钮时,他需要复制该所选调查中的所有数据。
我是Laravel的新手,所以我不知道该怎么做。但是我已经尝试了一些方法,但是没有用。而且我不知道为什么。有人可以帮我吗?
公共功能控制器:
public function copySurvey( Manager $fractal, SurveyTransformer $surveyTransformer ) {
// Copy function (Alleen nog niet volledig hier nog even aanwerken.)
$copy = Survey::first();
//copy attributes
$newsurvey = $copy->replicate();
$newsurvey->sid = 123456;
// save it
$newsurvey->save();
}
模型关系调查:
public function accuracy() {
return $this->hasOne( 'App\Models\LimeSurvey\Accuracy', 'survey_id', 'sid' );
}
public function groups() {
return $this->hasMany( 'App\Models\LimeSurvey\Group', 'sid', 'sid' )->orderBy('gid', 'asc')->orderBy('group_order', 'asc');
}
public function questions() {
return $this->hasMany( 'App\Models\LimeSurvey\Question', 'sid', 'sid' )->orderBy('gid', 'asc')->orderBy('question_order', 'asc');
}
public function parentQuestions() {
return $this->hasMany( 'App\Models\LimeSurvey\Question', 'sid', 'sid' )->where('parent_qid', '=', 0)->orderBy('gid', 'asc')->orderBy('question_order', 'asc');
}
public function languages() {
return $this->hasMany( 'App\Models\LimeSurvey\Language', 'surveyls_survey_id', 'sid' );
}
答案 0 :(得分:0)
保存后,您应该迭代问题关系:
$original = Survey::first();
$new = $original->replicate();
$new->sid = 123456;
$new->save();
foreach($original->groups as $group){
$new->groups()->attach($group);
}
foreach($original->questions as $question){
$new->questions()->attach($question);
}