mysqlworkbench格式化,Laravel 5.8更新MySQL JSON列强制转换为数组更改数据存储格式

I have a table with a json column cast as array. The Schema creation has the column defined like this:

$table->json('row_ids');

In my class:

protected $casts = [

'row_ids' => 'array',

];

I generate the data in this array / column by getting every row id from another table like this:

$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,

]);

When I dd a collection record I can see the columns in the target table OK, with one of the columns containing an array as expected:

#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, ▶"

]

When I check in MySQLWorkbench the data looks like this:

be008b1147bc54e6148a11656838b34c.png

In another controller I want to add or remove entries from the array in this column like this:

$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,

]);

Now the data in MySQLWorkbench looks like this:

9f2df1e3712300ddc3c027d6059d2576.png

Why does it get stored looking like a PHP array in the first instance, then later on update it gets stored as a json object?

Additionally the remove PHP code is working, yet the add is not, though it does not trigger an exception (you can see the first value is removed in the second image, but I cannot find it in the object in MySQL when I trigger the code to add it)

What did I miss? Thanks!

解决方案

The reason why it's saving differently is because array_diff returns an associative array whereas your initial data is an indexed array. Take the following for example:

$ids = [1, 2, 3, 4, 5];

$ids2 = [1, 2, 3];

Then if you perform an array_diff($ids, $ids2), it would return the following:

[

3 => 4,

4 => 5

]

So if you want to save as the same format as your initial one, you have to retrieve the values of the array using array_values:

$row_ids = array_values(array_diff($row_ids, $row_id_array));

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值