本文介绍了使用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" } }, { "emailAddress": { "name": "Test 2", "address": "test2@example" } } ]现在,我希望收集发送到"test@example"的所有电子邮件.我试过了:
Now I want a collection of all emails sent to "test@example". I tried:
DB::table('emails')->whereJsonContains('to->emailAddress->address', 'test@example')->get();(请参阅 laravel/docs/5.7/queries #json-where-clauses ) 但我没有比赛.是否有更好的方法使用Laravel(口才)进行搜索?
(see laravel/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\"'))推荐答案
箭头运算符不适用于数组.改用它:
The arrow operator doesn't work in arrays. Use this instead:
DB::table('emails') ->whereJsonContains('to', [['emailAddress' => ['address' => 'test@example']]]) ->get()更多推荐
使用Laravel在Json列中搜索
发布评论