【发布时间】:2018-07-25 10:51:39
【问题描述】:
我在 MySQL JSON 字段的数组(图像路径)中搜索值时遇到问题。我想检查图像的路径是否存储在数据库中以防止删除文件。
我尝试过的控制器功能:
public function checkImage($filename){
$content = Content::whereRaw('JSON_CONTAINS(images->"$.path", lAeH88gIv4HeycQXHeMbT8yOyydybbOVhsSq9eAx.jpeg')->exists();
$content = Content::whereRaw('json_contains(images->path, \'["lAeH88gIv4HeycQXHeMbT8yOyydybbOVhsSq9eAx.jpeg"]\')')->exists();
$content = Content::whereRaw('json_contains(images->path, \'["' . $filename . '"]\')')->exists();
$content = Content::whereRaw('json_contains(images, \'["' . $filename . '"]\')')->exists();
return response()->json($content);
}
示例 JSON:
{
"id": 4,
"images": [
{
"path": "abc/abc123.jpg",
"caption": "text",
"credits": "text"
},
{
"path": "abc/xyz123.jpg",
"caption": "text",
"credits": "text"
},
{
"path": "abc/827364527.jpg",
"caption": "text",
"credits": "text"
}
],
"created_at": "2017-12-20 11:40:16",
"updated_at": "2018-01-04 12:46:37"
}
【问题讨论】:
-
出了什么问题?你期望会发生什么?有错误信息吗?请提供minimal reproducible example。
标签: php mysql json lumen laravel-query-builder