dongtan9465
2014-10-02 11:52
浏览 27
已采纳

使用外键复制 - Laravel

I am trying to replicate certain data in my DB and I followed the steps as in the following link. Laravel 4: replicate to table However, I need to replicate some other data using only a foreign key.I tried to use the find() method to get my data but returned nothing.The where clause returns my data but in the form of array which isn't accepted by the replicate method.

Anhy idea what i am doing wrong and how can I replicate my other data?!

Code:

$item = Cv::find($cv_id);
        // return $item;
        $clone = $item->replicate();
        unset($clone['created_at'],$clone['updated_at']);
        $data = json_decode($clone, true);
        Cv::create($data);

        //Skills
        // return $cv_id;
        $skills = Skill::where('cv_id', $cv_id);
        $cloneSkills = $skills->replicate();
        unset($cloneSkills['created_at'],$cloneSkills['updated_at']);
        $skillData = json_decode($cloneSkills,true);
        Skill::create($skillData);

图片转代码服务由CSDN问答提供 功能建议

我正在尝试复制数据库中的某些数据,我按照以下链接中的步骤操作。 Laravel 4:复制到表格 但是,我只需要复制其他一些数据 我试图使用find()方法获取我的数据,但没有返回任何内容.where子句返回我的数据,但是以数组的形式返回复制方法。 \ n

Anhy知道我做错了什么以及如何复制我的其他数据?!

代码:

  $ item  = Cv :: find($ cv_id); 
 //返回$ item; 
 $ clone = $ item-> replicate(); 
 unset($ clone ['created_at'],$ clone ['updated_at'  ]); 
 $ data = json_decode($ clone,true); 
 Cv :: create($ data); 
 
 //技能
 //返回$ cv_id; 
 $技巧=技能::  where('cv_id',$ cv_id); 
 $ cloneSkills = $ skills-> replicate(); 
 unset($ cloneSkills ['created_at'],$ cloneSkills ['updated_at']); 
 $ skillData =  json_decode($ cloneSkills,true); 
 Skill :: create($ ski  llData); 
   
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douzaipou3327 2014-10-02 12:23
    已采纳

    For replicating skills you should probably use:

    $skills = Skill::where('cv_id', $cv_id)->get();
    
    foreach ($skills as $skill) {
    
        $cloneSkill = $skill->replicate();
        unset($cloneSkill['created_at'], $cloneSkill['updated_at']);
        $skillData = json_decode($cloneSkill, true);
        Skill::create($skillData);
    }
    

    You need to use get() to get all data and because $skills is Collection you need to use loop to replicate each skill.

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题