Php Laravel 5.8更新mysql json列强制转换为数组更改数据存储格式
我有一个带有Php Laravel 5.8更新mysql json列强制转换为数组更改数据存储格式,php,arrays,laravel,laravel-5.8,Php,Arrays,Laravel,Laravel 5.8,我有一个带有json列强制转换为数组的表。架构创建的列定义如下: $table->json('row_ids'); $id_array = DB::table($table->name)->pluck('api_id')->toArray(); TableChannelRow::create([ 'table_api_id' => $table->api_id, 'channel_api_id' => $channel->api
json
列强制转换为数组的表。架构创建的列定义如下:
$table->json('row_ids');
$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,
]);
$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,
]);
在我的班上:
protected $casts = [
'row_ids' => 'array',
];
我通过从另一个表中获取每个行id来生成此数组/列中的数据,如下所示:
$table->json('row_ids');
$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,
]);
$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,
]);
当我dd
收集记录时,我可以看到目标表中的列OK,其中一列包含预期的数组:
#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->json('row_ids');
$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,
]);
$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,
]);
在另一个控制器中,我希望在此列中添加或删除数组中的条目,如下所示:
$table->json('row_ids');
$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,
]);
$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
中的数据如下所示:
$table->json('row_ids');
$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,
]);
$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,
]);
为什么它在第一个实例中看起来像一个PHP数组,然后在以后的更新中被存储为一个json
对象
此外,remove PHP代码正在工作,而add则没有,尽管它不会触发异常(您可以在第二个图像中看到第一个值已被删除,但当我触发代码添加它时,我在MySQL中的对象中找不到它)
我错过了什么?谢谢 它以不同方式保存的原因是,返回的是关联数组,而初始数据是索引数组。以以下为例:
$ids = [1, 2, 3, 4, 5];
$ids2 = [1, 2, 3];
然后,如果执行数组_diff($ids,$ids2)
,它将返回以下内容:
[
3 => 4,
4 => 5
]
因此,如果要保存为与初始格式相同的格式,则必须使用以下方法检索数组的值:
我认为MySQL不支持数组列。所以“[1,2,3,4,5,…]”是无效的。嗯,谢谢。明白。当我以这种方式生成数组时,Laravel正在以这种格式转换数据。我第一次生成数据的方式搞砸了吗?第二天好像用得很好controller@TheRealPapa您是如何生成初始数据的?当您第一次创建
TableChannelRow
时,您将行ID设置为数组,您应该将其设置为数组的json编码版本。@Chin Leung hey!谢谢参见上面截取的第三个代码。我将ID提取到数组中,然后将它们存储在列中,该列被创建为json。你太棒了!非常感谢。因此,现在删除值后的存储格式是每第一个图像(PHP数组)。但你认为,为什么增加价值的努力不起作用呢?@Theralpapa Np!什么不适合推送添加?我看不到添加回存储在MySQL中的数组的值。谢谢你的帮助!在过去的两个晚上我学到了很多@Therealpapanp!是否将初始值改回了->toArray
,初始数据是否为索引数组?我的初始数组(存储)是[1,2,3,4,5,6,7,8,9,10,11,
。删除id 1后,它将变成[2,3,4,5,6,7,8,9,10,11,
,然后添加回值1(我正在使用AJAX切换)数组不包含值1。这是否回答了您的问题?