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()
{
//
}
}