Urgent.News

What's breaking now, across thousands of outlets.

Tech

Laravel replicate(): Duplicate Eloquent Models Safely

"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…

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.

One 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.

One 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.

Another 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.

Additionally, 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.

Finally, 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.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

Your Kimi K3.1 Pro Responses Truncate Silently

You point an existing OpenAI-compatible client at Kimi K3.1 Pro and it works on the first try. Then a long generation comes back cut off mid-sentence, and nothing in your code complained.

  • Kimi K3.1 Pro truncates responses silently
  • Detect truncation by checking finishreason
  • Choose correct variant to prevent truncation

5 Telegram Group Management Mistakes That Cost Me Members

I've been running Telegram communities for a while now — one peaked above 20,000 members — and I've made almost every mistake you can make with a group.

  • Neglected onboarding process, allowing unprotected chat entry
  • Vague rules document led to confusion and disputes
  • Aggressive keyword filtering censored legitimate messages

Four attributes left out of PraisonAI's subprocess blocklist

Four attributes left out of PraisonAI's subprocess blocklist Python sandboxes built from an attribute blocklist have a characteristic failure: the list is a description of what the author remembered…

  • Four attributes missing from Python sandbox blocklist in PraisonAI
  • Attributes: traceback, tbframe, fback, fbuiltins
  • Vulnerability identified in CVE-2026-39888 with high CVSS 3.1 score of 9.9

Insta360 Mic Officially Launched With E-Ink Display, Expandable Storage

The Insta360 Mic has officially launched, completing the brand’s wireless microphone lineup. It slots between the entry-level Mic Air and the flagship Mic Pro, adopting many of the fancier features from the latter. The device is aimed at creators who want a capable recording setup without having to commit to the flagship model.

More from Saturday 24 October →