使用Laravel在Json列中搜索

编程入门 行业动态 更新时间:2024-10-08 13:39:21
本文介绍了使用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列中搜索

本文发布于:2023-11-28 20:22:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1643824.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Laravel   Json

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!