0

如何将表行从一个表克隆到另一个我找到了克隆但不知道如何将其插入其他表的方法

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

 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!!');

    }
4

1 回答 1

2

当你复制 mysql 行时我解决了它,你得到了所有在 json 字符串中,所以你需要使用json_decode函数对其进行解码,然后再将其添加到数据库中,所以如果有人有同样的问题,这里是解决方案:)

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!!');

}
于 2014-07-25T12:51:05.340 回答