【发布时间】:2017-11-23 23:29:00
【问题描述】:
我一直在使用 Laravel 5.4 的 eloquent,但遇到了一个问题。
我有一个名为 posts 的数据库表,其中有一个名为 template_ids 的列。它以json_encoded 格式存储值,例如:
["1","25","48"]
现在,我想根据一组 ID 对我的查询应用过滤器:
$id_access = array:3 [
0 => "1"
1 => "3"
2 => "48"
]
我要做的是搜索数据库列template_ids中是否存在任何$id_access值。
我试过了:
Post::where('accessable_to',1)->whereIn('template_ids', $template_access_array)->paginate(3);
另外,我试过了:
Post::where('accessable_to',1)->whereRaw("JSON_CONTAINS(template_ids, ".$template_access.")")->paginate(3);
已查看this,但它不适合我。
【问题讨论】:
-
也许这个stackoverflow.com/questions/41942374/… 会帮助你。您还需要在 mysql 中将列 template_ids 的数据类型更改为 JSON
-
您遇到任何错误?
-
@AlankarMore 我已经添加了链接。我看过这个帖子
-
@RAUSHANKUMAR 是的,如果我使用包含。它给了我 json_contains 不存在。
-
检查你的mysql版本是否支持json_contains
标签: php mysql laravel laravel-5 eloquent