我在我的联系人表中存储了一个 JSON 数组,并且能够使用我的 SQL 管理器运行一条语句来删除一个键,但我还没有找到任何方法将这个查询转换为 Eloquent 或原始语句:
UPDATE `contacts` SET `additional` = JSON_REMOVE(`additional` , '$."3"')
对如何在 Laravel 中运行此查询有任何想法吗?
最佳答案
$id = 3;
$jsonPath = '$."'.$id.'"';
使用原始语句
DB::statement('UPDATE contacts SET additional = JSON_REMOVE(additional, ?)', [$jsonPath];
使用查询生成器:
DB::table('contacts')
->update([
'additional' => DB::raw("JSON_REMOVE(additional, '$jsonPath')")
]);
使用 Eloquent:
Contact::query()
->update([
'additional' => DB::raw("JSON_REMOVE(additional, '$jsonPath')")
]);
关于laravel - 使用 Eloquent 或 raw 语句删除多个 JSON 键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44186755/