承接 gabrielesbaiz/duplicate-toolkit 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

gabrielesbaiz/duplicate-toolkit

Composer 安装命令:

composer require gabrielesbaiz/duplicate-toolkit

包简介

A lightweight helper package to handle Eloquent model duplication.

README 文档

README

Latest Version on Packagist Total Downloads

A lightweight helper package to handle Eloquent model duplication.

Original code from neurony/laravel-duplicate

Features

  • ✅ Duplicate any Eloquent model record along with its underlying relationships.

Installation

You can install the package via composer:

composer require gabrielesbaiz/duplicate-toolkit

Usage

Step 1

Your Eloquent models should use the Gabrielesbaiz\DuplicateToolkit\Traits\HasDuplicates trait and the Gabrielesbaiz\DuplicateToolkit\Options\DuplicateOptions class.

The trait contains an abstract method getDuplicateOptions() that you must implement yourself.

Example:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Gabrielesbaiz\DuplicateToolkit\Options\DuplicateOptions;
use Gabrielesbaiz\DuplicateToolkit\Traits\HasDuplicates;

class YourModel extends Model
{
    use HasDuplicates;
    
    /**
     * Get the options for duplicating the model.
     *
     * @return DuplicateOptions
     */
    public function getDuplicateOptions(): DuplicateOptions
    {
        return DuplicateOptions::instance();
    }
}

Step 2

Once you've used the Gabrielesbaiz\DuplicateToolkit\Traits\HasDuplicates trait in your Eloquent models, you can duplicate model records by using the saveAsDuplicate() method present on that trait.

$model = YourModel::find($id);

$duplicatedModel = $model->saveAsDuplicate(); // returns the newly duplicated model instance

Customisations

Exclude certain columns

When duplicating a model, you can exclude certain columns from being duplicated by using the excludeColumns() method in your definition of the getDuplicateOptions() method.

The fields specified in the excludeColumns() method will be saved with their default value (null, false, 0, etc.)

/**
 * Get the options for duplicating the model.
 *
 * @return DuplicateOptions
 */
public function getDuplicateOptions() : DuplicateOptions
{
    return DuplicateOptions::instance()
        ->excludeColumns('column_one', 'column_two');
}

Specify unique columns

When duplicating a model, you can save certain columns in an unique format by using the uniqueColumns() method in your definition of the getDuplicateOptions() method.

The fields specified in the uniqueColumns() method will be saved in a unique format by appending (n) at the end.
Example: original name (1), original name (2)

/**
 * Get the options for duplicating the model.
 *
 * @return DuplicateOptions
 */
public function getDuplicateOptions() : DuplicateOptions
{
    return DuplicateOptions::instance()
        ->uniqueColumns('column_one', 'column_two');
}

Exclude entire relations

By default, when duplicating a model, all of its "child" relations are also duplicated along with it.

You can exclude certain relations from being duplicated by using the excludeRelations() method in your definition of the getDuplicateOptions() method.

The relations specified in the excludeRelations() method will not be duplicated along with the targeted model, meaning that the newly duplicated model will not have any records associated to it for the specified relations.

/**
 * Get the options for duplicating the model.
 *
 * @return DuplicateOptions
 */
public function getDuplicateOptions() : DuplicateOptions
{
    return DuplicateOptions::instance()
        ->excludeRelations('relationOne', 'relationTwo');
}

Exclude certain columns from certain relations

When duplicating a model, you can exclude certain columns of its "child" relations from being duplicated by using the excludeRelationColumns() method in your definition of the getDuplicateOptions() method.

This method accepts only one parameter which should be an associative array containing:
key -> the name of a relation
value -> an array containing the columns to exclude for that relation

The fields specified in the excludeRelationColumns() method will be saved with their default value (null, false, 0, etc.)

/**
 * Get the options for duplicating the model.
 *
 * @return DuplicateOptions
 */
public function getDuplicateOptions() : DuplicateOptions
{
    return DuplicateOptions::instance()
        ->excludeRelationColumns([
            'relationOne' => ['column_one', 'column_two'],
            'relationTwo' => ['column_one'],
        ]);
}

Specify unique columns for certain relations

When duplicating a model, you can save certain columns of its "child" relations in an unique format by using the uniqueRelationColumns() method in your definition of the getDuplicateOptions() method.

This method accepts only one parameter which should be an associative array containing:
key -> the name of a relation
value -> an array containing the unique columns for that relation

The fields specified in the uniqueRelationColumns() method will be saved in an unique format by appending (n) at the end.
Example: original relation name (1), original relation name (2)

/**
 * Get the options for duplicating the model.
 *
 * @return DuplicateOptions
 */
public function getDuplicateOptions() : DuplicateOptions
{
    return DuplicateOptions::instance()
        ->uniqueRelationColumns([
            'relationOne' => ['column_one', 'column_two'],
            'relationTwo' => ['column_one'],
        ]);
}

Duplicate only the targeted model

If you only want to duplicate your targeted model without duplicating any relations whatsoever, you can specify this by using the disableDeepDuplication() method in your definition of the getDuplicateOptions() method.

When using this method, all relations of all types will be ignored when duplicating the model.

/**
 * Get the options for duplicating the model.
 *
 * @return DuplicateOptions
 */
public function getDuplicateOptions() : DuplicateOptions
{
    return DuplicateOptions::instance()
        ->disableDeepDuplication();
}

Events

The duplicate functionality comes packed with two Eloquent events: duplicating and duplicated

You can implement these events in your Eloquent models as you would implement any other Eloquent events that come with the Laravel framework.

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Gabrielesbaiz\DuplicateToolkit\Options\DuplicateOptions;
use Gabrielesbaiz\DuplicateToolkit\Traits\HasDuplicates;

class YourModel extends Model
{
    use HasDuplicates;

    /**
     * Boot the model.
     *
     * @return DuplicateOptions
     */
    public static function boot()
    {
        parent::boot();

        static::duplicating(function ($model) {
            // your logic here
        });

        static::duplicated(function ($model) {
            // your logic here
        });
    }
    
    /**
     * Get the options for duplicating the model.
     *
     * @return DuplicateOptions
     */
    public function getDuplicateOptions(): DuplicateOptions
    {
        return DuplicateOptions::instance();
    }
}

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

gabrielesbaiz/duplicate-toolkit 适用场景与选型建议

gabrielesbaiz/duplicate-toolkit 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9.8k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 03 月 04 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laravel」 「Gabriele Sbaiz」 「duplicate-toolkit」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 gabrielesbaiz/duplicate-toolkit 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 gabrielesbaiz/duplicate-toolkit 我们能提供哪些服务?
定制开发 / 二次开发

基于 gabrielesbaiz/duplicate-toolkit 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 9.8k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 22
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-03-04