使用Laravel在Json列中搜索 [英] Search in Json column with Laravel
本文介绍了使用Laravel在Json列中搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的电子邮件表中,我有一列名为To
且列类型为Json
的列.值的存储方式如下:
In my emails table, I have a column named To
with column-type Json
. This is how values are stored:
[
{
"emailAddress": {
"name": "Test",
"address": "test@example.com"
}
},
{
"emailAddress": {
"name": "Test 2",
"address": "test2@example.com"
}
}
]
现在,我希望收集发送到"test@example.com"的所有电子邮件.我试过了:
Now I want a collection of all emails sent to "test@example.com". I tried:
DB::table('emails')->whereJsonContains('to->emailAddress->address', 'test@example.com')->get();
(请参阅 https://laravel.com/docs/5.7/queries #json-where-clauses ) 但我没有比赛.是否有更好的方法使用Laravel(口才)进行搜索?
(see https://laravel.com/docs/5.7/queries#json-where-clauses) but I do not get a match. Is there a better way to search using Laravel (Eloquent)?
在调试栏中,我可以看到此查询被翻译"为:
In the debugbar, I can see that this query is "translated" as:
select * from `emails` where json_contains(`to`->'$."emailAddress"."address"', '\"test@example.com\"'))
推荐答案
箭头运算符不适用于数组.改用它:
The arrow operator doesn't work in arrays. Use this instead:
DB::table('emails')
->whereJsonContains('to', [['emailAddress' => ['address' => 'test@example.com']]])
->get()
这篇关于使用Laravel在Json列中搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文