Make a Copy of the Model

If you have two very similar Models (like shipping address and billing address) and you need to make a copy of one to another, you can use replicate() method and change some properties after that.

Example from the official docs:

1$shipping = Address::create([
2 'type' => 'shipping',
3 'line_1' => '123 Example Street',
4 'city' => 'Victorville',
5 'state' => 'CA',
6 'postcode' => '90001',
7]);
8 
9$billing = $shipping->replicate()->fill([
10 'type' => 'billing'
11]);
12 
13$billing->save();

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 22 courses (477 lessons, total 38 h 20 min)
  • 2 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord