Php Larvel查询以选择JSON字段中的列表项
我使用LaravelV6.11.0、v2.9.3和v0.1.13将动态数据存储在MySQL JSON字段中。存储的数据与此类似:Php Larvel查询以选择JSON字段中的列表项,php,mysql,laravel,eloquent,Php,Mysql,Laravel,Eloquent,我使用LaravelV6.11.0、v2.9.3和v0.1.13将动态数据存储在MySQL JSON字段中。存储的数据与此类似: [ { "layout": "source", "key": "5ce8e0a877487fe5", "attributes": { "value": "342", "unit": "USD", "language": "en", "url": "http:\\/\\/google.com",
[
{
"layout": "source",
"key": "5ce8e0a877487fe5",
"attributes": {
"value": "342",
"unit": "USD",
"language": "en",
"url": "http:\\/\\/google.com",
"authority": "google.com",
"entry_date": "2020-01-21",
"date": "2019-12-21"
}
},
{
"layout": "source",
"key": "a82393ce016e8c14",
"attributes": {
"value": "444",
"unit": "USD",
"language": "en",
"entry_date": "2020-01-21",
"url": "https:\\/\\/google.com",
"authority": "TEST",
"date": "2020-01-20"
}
}
]
我想知道是否可以构建一个Laravel查询,根据权限
条目选择第二个条目?标准是:
应包含url
和google.com
不应该是authority
google.com
$data = App\YourModel::whereRaw('JSON_CONTAINS(body->"$[*].attributes.url", "\"https://google.com\"")')
->orWhereRaw('JSON_CONTAINS(body->"$[*].id", "\"http://google.com\"")')
->whereRaw('not JSON_CONTAINS(body->"$[*].attributes.authority", "\"google.com\"")')
->get();
foreach ($data as $key => $row) {
$bodyArr = json_decode($row->body);
foreach ($bodyArr as $item) {
if ((preg_match("/(http:\/\/www\.|https?:\/\/google.com)/i",$item->attributes->url)) && (strpos($item->attributes->authority, 'google.com') === false)) {
dump($item);
// YOUR CODE GOES HERE
}
}
}
您是存储整个数组还是每次存储一个对象?完整的JSON在一个MySQL列中-这就是组件自动存储它的方式。那么db列类型呢?它是文本还是json?它是json列