HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers95/rentman.io/database/migrations/2022_05_09_155536_edit_card_component.php
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;

class EditCardComponent extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        /**
         * Insert a value or key/value pair after a specific key in an array.  If key doesn't exist, value is appended
         * to the end of the array.
         *
         * @param array $array
         * @param string $key
         * @param array $new
         *
         * @return array
         */
        function array_insert_after(array $array, $key, array $new)
        {
            $keys = array_keys($array);
            $index = array_search($key, $keys);
            $pos = false === $index ? count($array) : $index + 1;

            return array_merge(array_slice($array, 0, $pos), $new, array_slice($array, $pos));
        }

        $cardComponentRows = DB::table('components')->where('component_type_id', 6)->get();
        $cardComponentRows->each(function ($cardComponentRow) {
            $rowData = json_decode($cardComponentRow->data, true);
            $rowDataKeys = array_keys($rowData);

            $keyToInsert = str_replace('row_heading', 'hover_layout', $rowDataKeys[3]);
            $valueToInsert = '1';

            $rowData = array_insert_after($rowData, $rowDataKeys[3], [$keyToInsert => $valueToInsert]);

            DB::table('components')
                ->where('id', $cardComponentRow->id)
                ->limit(1)
                ->update(['data' => json_encode($rowData)]);
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        //
    }
}