Laravel 4:复制到表 [英] Laravel 4: replicate to table

查看:195
本文介绍了Laravel 4:复制到表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将一个表中的表行克隆到另一个表中,我发现了进行克隆的方法,但不知道如何将其插入另一张表中

how to clone table row from one table to another i found way to make a clone but dont know how to insert it in other table

我有数据类和产品类,我只想从数据克隆到产品一行

I have Data class and Product class and I want to clone from Data to Product one row only

 public function getClone($id) {

        $item = Data::find($id);
        $clone = $item->replicate();
        unset($clone['created_at'],$clone['updated_at']);

        $product = new Product;

      --> what goes here i tried $product->fill($clone); But i get error: 
          must be of the type array, object given

        return Redirect::to('admin/content')
            ->with('message', 'Clone Created!!');

    }

推荐答案

我解决了当您获取mysql行的副本时,您将所有json字符串都包含在内,因此您需要先使用 json_decode 函数对其进行解码再次将其添加到数据库中,因此如果有人遇到相同的问题,这是解决方案:)

I solved it when u get replicate of mysql row you get all inside json string so u need to decode it with json_decode function before adding it to database again so here is solution if someone has same problem :)

public function getClone($id) {

    $item = Data::find($id);
    $clone = $item->replicate();
    unset($clone['created_at'],$clone['updated_at']);

      $data = json_decode($clone, true);
      Product::create($data);

    return Redirect::to('admin/content')
        ->with('message', 'Clone Created!!');

}

这篇关于Laravel 4:复制到表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆