Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

philipbaginski's avatar

Duplicate record

Hi everyone! I would like to duplicate records. I hava a piece of code (below), but with this code I'm not able to duplicate more than one record. I have to duplicate 9-11 record at once.

Any idea what to change to make it work?

<?php
namespace App\Nova\Actions;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Collection;
use Laravel\Nova\Actions\Action;
use Laravel\Nova\Fields\ActionFields;

class DuplicateResource extends Action {

    use SerializesModels;

    public $showOnDetail = true;
    public $showOnIndex = true;
    public $showOnTableRow = false;
    public $confirmButtonText = '...';
    public $cancelButtonText = '...';
    public $confirmText = '...';
    public $withoutActionEvents = true;
    protected $keepRelations = [];
    protected $duplicateRelations = [];

    /**
     * Perform the action on the given models.
     *
     * @param ActionFields $fields
     * @param Collection   $models
     *
     * @return mixed
     */
    public function handle(ActionFields $fields, Collection $models)
    {


        $model = $models->first();
        $newModel = $model->replicate();
        // Override values from fields
        foreach ($fields->getAttributes() as $key => $value) {
            if(isset($value)){
                $newModel->$key = $value;
            }
        }
        $newModel->push();
        if (!empty($this->duplicateRelations)) {
            // load the relations
            $model->load($this->duplicateRelations);
            foreach ($model->getRelations() as $relation => $items) {
                // works for hasMany
                foreach ($items as $item) {
                    // clean up our models, remove the id and remove the appends
                    unset($item->id);
                    $item->setAppends([]);
                    // create a relation on the new model with the data.
                    $newModel->{$relation}()->create($item->toArray());
                }
            }
        }
        if (!empty($this->keepRelations)) {
            // load the fresh model with relations to maintain
            unset($model->relations);
            $model->load($this->keepRelations);
            foreach ($model->getRelations() as $relation => $items) {
                // works for hasMany
                foreach ($items as $item) {
                    $newModel->{$relation}()->attach($item);
                }
            }
        }
        $newModel->save();
        return Action::message($this->getSuccessMessage($model, $newModel, $fields));
    }
    protected function getSuccessMessage(Model $originalModel, Model $newModel, ActionFields $fields) : String {
        return "...";
    }

    /**
     * Get the fields available on the action.
     *
     * @return array
     */
    public function fields()
    {
        return [];
    }
}
0 likes
0 replies

Please or to participate in this conversation.