Duplicate entities in October CMS

When creating content in OctoberCMS, it is often necessary to copy (clone) some Model.
Or to put it simply in the language of your customers: How do I copy an existing record? Given the frequency of requests for this functionality, I want to give the idea to generate this block of code by default in Rainlab.Builder 😎

First, find the necessary controller of your plugin, for example plugins/icstudio/control/controllers/Tariffs.php

Now let's add a new method to it that will create copies of the required Mods. On the Internet you can find similar code, but we have finalized it, so it is not recommended to use copied.
- number 2 will be added to the record name;
- number 2 will be added to slug;
- the is_active key will be set to 0. This will keep your production from outputting wrong data.

public function onDuplicate()
$checked_items_ids = input('checked');
foreach ($checked_items_ids as $id) {
$original = TariffModel::where("id", $id)->first();
$clone = $original->replicate();
$clone->name = $clone->name . '2';
$clone->slug = $clone->slug . '2';
$clone->service_name = $clone->service_name . '2';
$clone->is_active = 0;
Flash::success('Записи скопированы');
return $this->listRefresh();

Now you need to bind this action to the backend layout. For this purpose find file _list_toolbar.htm for your controller, for example for controller from the example the path to this file will be: plugins/icstudio/control/controllers/tariffs/_list_toolbar.htm

Let's add this code to output the button:

class="btn btn-default oc-icon-clone"
onclick="$(this).data('request-data', {
checked: $('.control-list').listWidget('getChecked')
data-request-confirm="Хотите скопировать указанные записи?"
data-trigger=".control-list input[type=checkbox]"
data-request-success="$(this).prop('disabled', true)"
Копировать выбранные записи

The code for the button must be inside


Save, clear the cache and enjoy:

It's never too late to change your business for the better

Get start

This site uses cookies. We do not personalize you, but only make surfing the site more convenient. You can check out our Privacy Policy