In Laravle 6, set the refactoring of mysql field type JSON value (implemented based on the helper function arr )
1. Existing code implementation
if ($themeInstallationTask->setting_migrations) {
$settingMigrations = $themeInstallationTask->setting_migrations;
}
$settingMigrations[] = [
'file' => $availableVersion . '/' . $this->migrateSettingsDataFile,
'exec_at' => now()->utc()->toDateTimeString()
];
$themeInstallationTask->setting_migrations = $settingMigrations;
$themeInstallationTask->save();
2. The final generated structure is as follows, which is an array with the field name: SETTING_MIGRATIONS
[
{
"file": "v2.0.15-rc.6/migrate_settings_data.php",
"exec_at": "2022-10-31 09:45:06"
},
{
"file": "v2.0.15-rc.7/migrate_settings_data.php",
"exec_at": "2022-10-31 09:45:06"
},
{
"file": "v2.0.15/migrate_settings_data.php",
"exec_at": "2022-10-31 09:45:06"
},
{
"file": "v2.0.17-rc.0/migrate_settings_data.php",
"exec_at": "2022-10-31 09:45:06"
}
]
3. Decide to adjust the name of the adjustment field as extra, and include the value of the setting_migrations field. The code is adjusted as follows, based on the helper function ar.
$extra = $themeInstallationTask->extra;
if (Arr::has($extra, 'setting_migrations')) {
$settingMigrations = Arr::get($extra, 'setting_migrations');
}
$settingMigrations[] = [
'file' => $file,
'exec_at' => now()->utc()->toDateTimeString()
];
Arr::set($extra, 'setting_migrations', $settingMigrations);
$themeInstallationTask->extra = $extra;
$themeInstallationTask->save();
4. The final generated structure is as follows, is an object, and the field name is EXTRA. as shown in Figure 1
{
"setting_migrations": [
{
"file": "v2.0.15-rc.6/migrate_settings_data.php",
"exec_at": "2022-10-31 09:45:06"
},
{
"file": "v2.0.15-rc.7/migrate_settings_data.php",
"exec_at": "2022-10-31 09:45:06"
},
{
"file": "v2.0.15/migrate_settings_data.php",
"exec_at": "2022-10-31 09:45:06"
},
{
"file": "v2.0.17-rc.0/migrate_settings_data.php",
"exec_at": "2022-10-31 09:45:06"
}
]
}
