Search & Replace against collections

We’ve been using cockpit in a test environment with some WYSIWIG fields. These fields contain absolute URLs which will need to be mass-replaced when we migrate. (e.g. “” will need to be replaced with “”) inside some Collections and Singletons.

What’s the best way to iterate through Collection entries and Singletons to do a search/replace?

simple approach: create a migrate script e.g. migrate.php.



$collection = '{collectionname}';
$records = cockpit('collections')->find($collection); // get all entries

foreach ($records as $record) {
    $record['wysiwyg'] = str_replace('xyz', 'zyx', $record['wysiwyg']);

    cockpit('collections')->save($collection, $record);
1 Like