Laravel 5.8 更新 mysql json 列转换为数组更改数据存储格式
Laravel 5.8 update mysql json column cast as array changes data stored format
我有一个 table,其中一个 json
列转换为数组。架构创建的列定义如下:
$table->json('row_ids');
在我的 class:
protected $casts = [
'row_ids' => 'array',
];
我通过从另一个 table 获取每一行 ID 来生成此数组/列中的数据,如下所示:
$id_array = DB::table($table->name)->pluck('api_id')->toArray();
TableChannelRow::create([
'table_api_id' => $table->api_id,
'channel_api_id' => $channel->api_id,
'row_ids' => $id_array,
]);
当我 dd
一个集合记录时,我可以看到目标中的列 table 好的,其中一列包含预期的数组:
#attributes: array:4 [▼
"api_id" => 2
"table_api_id" => 1
"channel_api_id" => 6
"row_ids" => "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, ▶"
]
当我签入 MySQLWorkbench
时,数据如下所示:
在另一个控制器中,我想在此列的数组中添加或删除条目,如下所示:
$table_channel_row = TableChannelRow::where('table_api_id', '=', $rowId)
->where('channel_api_id', '=', $channelId)
->first();
$row_ids = $table_channel_row->row_ids;
if ($is_checked == 'yes') {
// Add value to array if it does not already exist
if (!in_array($row_id, $row_ids)) {
array_push($row_ids, $row_id);
}
} else {
// Remove value from array
$row_id_array[] = $row_id;
$row_ids = array_diff($row_ids, $row_id_array);
}
$table_channel_row->update([
'row_ids' => $row_ids,
]);
现在 MySQLWorkbench
中的数据如下所示:
为什么它在第一个实例中看起来像一个 PHP 数组,然后在更新后它被存储为一个 json
对象?
此外,删除 PHP 代码有效,但添加无效,尽管它不会触发异常(您可以看到第二张图片中的第一个值被删除,但我在当我触发代码添加它时 MySQL 中的对象)
我错过了什么?谢谢!
保存方式不同的原因是 array_diff return 是一个关联数组,而您的初始数据是一个索引数组。以下面的例子:
$ids = [1, 2, 3, 4, 5];
$ids2 = [1, 2, 3];
然后,如果您执行 array_diff($ids, $ids2)
,它将 return 以下内容:
[
3 => 4,
4 => 5
]
因此,如果您想保存为与初始格式相同的格式,则必须使用 array_values:
检索数组的值
$row_ids = array_values(array_diff($row_ids, $row_id_array));
我有一个 table,其中一个 json
列转换为数组。架构创建的列定义如下:
$table->json('row_ids');
在我的 class:
protected $casts = [
'row_ids' => 'array',
];
我通过从另一个 table 获取每一行 ID 来生成此数组/列中的数据,如下所示:
$id_array = DB::table($table->name)->pluck('api_id')->toArray();
TableChannelRow::create([
'table_api_id' => $table->api_id,
'channel_api_id' => $channel->api_id,
'row_ids' => $id_array,
]);
当我 dd
一个集合记录时,我可以看到目标中的列 table 好的,其中一列包含预期的数组:
#attributes: array:4 [▼
"api_id" => 2
"table_api_id" => 1
"channel_api_id" => 6
"row_ids" => "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, ▶"
]
当我签入 MySQLWorkbench
时,数据如下所示:
在另一个控制器中,我想在此列的数组中添加或删除条目,如下所示:
$table_channel_row = TableChannelRow::where('table_api_id', '=', $rowId)
->where('channel_api_id', '=', $channelId)
->first();
$row_ids = $table_channel_row->row_ids;
if ($is_checked == 'yes') {
// Add value to array if it does not already exist
if (!in_array($row_id, $row_ids)) {
array_push($row_ids, $row_id);
}
} else {
// Remove value from array
$row_id_array[] = $row_id;
$row_ids = array_diff($row_ids, $row_id_array);
}
$table_channel_row->update([
'row_ids' => $row_ids,
]);
现在 MySQLWorkbench
中的数据如下所示:
为什么它在第一个实例中看起来像一个 PHP 数组,然后在更新后它被存储为一个 json
对象?
此外,删除 PHP 代码有效,但添加无效,尽管它不会触发异常(您可以看到第二张图片中的第一个值被删除,但我在当我触发代码添加它时 MySQL 中的对象)
我错过了什么?谢谢!
保存方式不同的原因是 array_diff return 是一个关联数组,而您的初始数据是一个索引数组。以下面的例子:
$ids = [1, 2, 3, 4, 5];
$ids2 = [1, 2, 3];
然后,如果您执行 array_diff($ids, $ids2)
,它将 return 以下内容:
[
3 => 4,
4 => 5
]
因此,如果您想保存为与初始格式相同的格式,则必须使用 array_values:
检索数组的值$row_ids = array_values(array_diff($row_ids, $row_id_array));