Search code examples
phplaravel-4duplicatesclonereplicate

Laravel 4: replicate to table


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

    }

Solution

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