{"id":20410020,"url":"https://github.com/vdlp/eloquent-model-cloner","last_synced_at":"2026-04-23T03:32:20.937Z","repository":{"id":57077179,"uuid":"281896675","full_name":"vdlp/eloquent-model-cloner","owner":"vdlp","description":null,"archived":false,"fork":false,"pushed_at":"2021-06-17T15:53:33.000Z","size":24,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":4,"default_branch":"develop","last_synced_at":"2025-09-17T14:21:11.542Z","etag":null,"topics":["laravel","trait"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/vdlp.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-07-23T08:34:01.000Z","updated_at":"2021-06-17T15:53:37.000Z","dependencies_parsed_at":"2022-08-24T12:50:52.906Z","dependency_job_id":null,"html_url":"https://github.com/vdlp/eloquent-model-cloner","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/vdlp/eloquent-model-cloner","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vdlp%2Feloquent-model-cloner","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vdlp%2Feloquent-model-cloner/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vdlp%2Feloquent-model-cloner/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vdlp%2Feloquent-model-cloner/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vdlp","download_url":"https://codeload.github.com/vdlp/eloquent-model-cloner/tar.gz/refs/heads/develop","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vdlp%2Feloquent-model-cloner/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32164949,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-23T02:19:40.750Z","status":"ssl_error","status_checked_at":"2026-04-23T02:17:55.737Z","response_time":53,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["laravel","trait"],"created_at":"2024-11-15T05:44:37.483Z","updated_at":"2026-04-23T03:32:20.921Z","avatar_url":"https://github.com/vdlp.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Eloquent model cloner\n\nThis package was inspired by https://github.com/BKWLD/cloner and modified to also work with OctoberCMS installations.\n\nA trait for Laravel Eloquent models that lets you clone a model and it's relationships, including files. Even to another database.\n\n## Installation\n\nTo get started with the Eloquent Model Cloner, use Composer to add the package to your project's dependencies:\n\n```\ncomposer require vdlp/eloquent-model-cloner\n```\n\n## Usage\n\nYour model should now look like this:\n\n```php\nclass Article extends Eloquent {\n   use \\Vdlp\\EloquentModelCloner\\Cloneable;\n}\n```\n\nYou can clone an Article model like so:\n\n```php\n$clone = Article::first()-\u003eduplicate();\n```\n\n### Cloning Relationships\n\nLets say your `Article` has many `Photos` (a one to many relationship) and can have more than one `Authors` (a many to many relationship). Now, your `Article` model should look like this:\n\n```php\nclass Article extends Eloquent {\n   use \\Vdlp\\EloquentModelCloner\\Cloneable;\n\n   protected $cloneableRelations = ['photos', 'authors'];\n\n   public function photos() {\n       return $this-\u003ehasMany('Photo');\n   }\n\n   public function authors() {\n        return $this-\u003ebelongsToMany('Author');\n   }\n}\n```\n\nThe `$cloneableRelations` informs the `Cloneable` as to which relations it should follow when cloning.\nNow when you call `Article::first()-\u003eduplicate()`, all of the `Photo` rows of the original will be copied and associated with the new `Article`.\nAnd new pivot rows will be created associating the new `Article` with the `Authors` of the original (because it is a many to many relationship, no new `Author` rows are created).\nFurthermore, if the `Photo` model has many of some other model, you can specify `$cloneableRelations` in its class and `Cloner` will continue replicating them as well.\n\n### Customizing the cloned attributes\n\nBy default, `Cloner` does not copy the `id` (or whatever you've defined as the `key` for the model) field; it assumes a new value will be auto-incremented.\nIt also does not copy the `created_at` or `updated_at`.\nYou can add additional attributes to ignore as follows:\n\n```php\nclass Photo extends Eloquent {\n   use \\Vdlp\\EloquentModelCloner\\Cloneable;\n\n   protected $cloneExemptAttributes = ['uid', 'source'];\n\n   public function article() {\n        return $this-\u003ebelongsTo('Article');\n   }\n\n   public function onCloning($src, $child = null) {\n        $this-\u003euid = str_random();\n\n        if ($child) {\n            echo 'This was cloned as a relation!';\n        }\n\n        echo 'The original key is: ' . $src-\u003egetKey();\n   }\n}\n```\n\nThe `$cloneExemptAttributes` adds to the defaults.\nIf you want to replace the defaults altogether, override the trait's `getCloneExemptAttributes()` method and return an array.\n\nAlso, note the `onCloning()` method in the example.\nIt is being used to make sure a unique column stays unique.\nThe `Cloneable` trait adds to no-op callbacks that get called immediately before a model is saved during a duplication and immediately after: `onCloning()` and `onCloned()`.\nThe `$child` parameter allows you to customize the behavior based on if it's being cloned as a relation or direct.\n\nIn addition, Cloner fires events during cloning and when the model has been cloned, see:\n\n- `\\Vdlp\\EloquentModelCloner\\Events\\Cloned`\n- `\\Vdlp\\EloquentModelCloner\\Events\\Cloning`\n\nThe event payload contains the clone and the original model instances.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvdlp%2Feloquent-model-cloner","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvdlp%2Feloquent-model-cloner","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvdlp%2Feloquent-model-cloner/lists"}