{
  "id": 16263470,
  "title": "Laravel replicate(): Duplicate Eloquent Models Safely",
  "url": "https://urgent.news/2026/10/24/laravel-replicate-duplicate-eloquent-models-safely",
  "topic": "tech",
  "section": "Tech",
  "published": "2026-10-24T02:26:18.000Z",
  "source": {
    "name": "Dev.to",
    "slug": "dev-to",
    "url": "https://dev.to/devtalk94/laravel-replicate-duplicate-eloquent-models-safely-13i8"
  },
  "original_language": "en",
  "account": "Copying Eloquent models in Laravel using the replicate() method can be done safely, but there are some important caveats to consider. The method returns an unsaved copy of the original model, which means that nothing is written to the database until you explicitly call the save() method.\n\nOne thing to be aware of is that replicate() automatically skips certain columns when creating the copy. These include the primary key, the created_at and updated_at columns, any columns listed in the uniqueIds() method, and any columns decorated with the #[Refreshes] attribute. In addition, you can pass an array of column names to the replicate() method to explicitly exclude them from being copied.\n\nOne potential trap with replicate() is that it can copy unique columns, such as a slug. If the slug already exists in the database, attempting to save the copy will result in a Duplicate entry error. To avoid this issue, you can manually exclude the unique columns or modify their values before saving the copy.\n\nAnother trap is that replicate() copies all attribute values, including secrets and counters. If a post has an API token and a view count, duplicating the post using replicate() will result in the new copy having the same token and view count as the original. To avoid this, you should explicitly exclude any sensitive or countable columns from the copy.\n\nAdditionally, if you try to duplicate a soft-deleted record using replicate(), the copy will also be soft-deleted, as the deleted_at timestamp is copied as well. To avoid this, you can include the deleted_at column in the list of excluded columns when using replicate() on a trashed record.\n\nFinally, when duplicating records that have related models, such as a post with comments, the copy will only contain the new post without any associated comments. To duplicate the related models as well, you need to replicate each related model explicitly and attach it to the new copy. It is recommended to perform this operation inside a transaction to ensure that all changes are committed together and to avoid any inconsistencies in the database.",
  "summary": "\"Duplicate this\" buttons look easy: copy the row, save it, done. Then the first copy fails with a duplicate-key error, or the new post comes out already deleted, or it quietly carries over an API token and a view count from the original. Eloquent has a built-in method for this, replicate() , and it handles more than people expect. It also leaves a few traps. I ran every example on Laravel 13.35.0…",
  "key_points": [
    "replicate() creates an unsaved copy of Eloquent models in Laravel",
    "Skips primary key, timestamps, uniqueIds(), and #[Refreshes] attributes",
    "Be cautious of copying unique columns to avoid duplicate entry errors"
  ],
  "editors_take": "Relying on the replicate method without manual exclusions risks data integrity errors and the unintended duplication of sensitive information or unique database constraints.",
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "also_reported_by": []
  },
  "ai_generated": true,
  "disclaimer": "Summaries, key points and the editor’s take are written by software from other outlets’ reporting and may contain errors — always check the linked original."
}