
Laravel clone model
You can create a clone of model in Laravel using the replicate() method. You can get the model instance using find() method or you can also create a model instance using create() method after that you can create a new clone from that model using replicate() method.
Use below code to create a duplicate record in users table of Laravel application. You have to just place the code in your controller's method. It will create an another record in your users table.
User::find($id)->replicate()->save();
You can also clone a record to another table when you have model instances that share many of the same attributes. You may create an unsaved copy of an existing model instance using the replicate method and save to another model.
Here we have an address model and we have created a record in addresss table using create() method and after that we have created a duplicate record from it using replicate() method and update the "type" column value with "billing" and save the record in billing table.
$shipping = Address::create([
'type' => 'shipping',
'line_1' => '123 Example Street',
'city' => 'Victorville',
'state' => 'CA',
'postcode' => '90001',
]);
$billing = $shipping->replicate()->fill([
'type' => 'billing'
]);
$billing->save();
-
Make Copy or Duplicate table row in laravel using replicate method
--PATH app\Http\Controllers\<YourController.php>Post::find($id)->replicate()->save();
This code snippet makes an exact copy or duplicate table row in laravel, and saves it into the database with unique id value (which is a primary id of table with auto increment) and it changes the created_at and updated_at field value with current timestamp value as per your table structure.
-
Clone model and modify existing data in laravel
--PATH app\Http\Controllers\<YourController.php>$new_title = "This is a new title for post"; $new_data = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes"; $post = Post::find($id); $newPost = $post->replicate(); $newPost->title = $new_title; $newPost->data = $new_data; $newPost->save();
Clone model and modify existing data in laravel, If you want to make a copy of a row in a table and also change some column data before saving it to the database. You can change it by assigning a value to column using column name on a newly created model instance after replicate method. All the values of columns of the newly created post will be the same except the title and data or changed as per your table structure like id (which is a primary key with auto increment), created_at and updated_at fields (current timestamp).
-
Replicate model and it’s relationship
--PATH app\Http\Controllers\<YourController.php>$post = Post::find($id); $newPost = $post->replicate(); $newPost->push(); foreach($post->tags as $tag) { $newPost->tags()->attach($tag); } foreach($post->categories as $category) { $newPost->categories()->attach($category); }
If your post has categories and tags. You can copy it and save it to the database. You can use the attach() method on a models relation, to insert categories and tags on the pivot table to the newly created record. You have to get id of post which you want to make duplicate and assign to $id variable.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Laravel
- RuntimeException You must enable the openssl extension in your php.ini to load information from https://repo.packagist.org
- Print query in laravel
- Target class [HomeController] does not exist
- The use statement with non-compound name 'DB' has no effect
- How to validate URL with https using regex in laravel
- Display success message in laravel
- Datetime field in Laravel migration
- How to create controller in laravel
- Laravel migration add foreign key to existing table
- Get products with number of orders in Laravel
- Use withCount() to get total number of records with relationship
- Laravel get count with where condition
- Drop foreign key column in Laravel using migration
- Route not defined in Laravel
- How to pass link from controller to view in laravel on ajax call
- How to call Laravel route in jQuery
- How to check data inserted or deleted in pivot after toggle method
- FirstOrCreate() Not Inserting Model
- How to pass data to partial view file in laravel
- How to check duplicate entry in laravel
- How to pass external link in laravel blade to anchor tag
- Fatal error: Uncaught Error: Class "Illuminate\Foundation\Application" not found
- Argument 1 passed to Symfony\Component\HttpFoundation\Response::setContent() must be of the type string or null, object given
- Calculate age from date of birth in Laravel
- Get duplicate records in laravel