Laravel 4: replicate to table

拈花ヽ惹草 提交于 2019-12-12 02:48:14

问题


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

    }

回答1:


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

}


来源:https://stackoverflow.com/questions/24469259/laravel-4-replicate-to-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!