简体   繁体   English

使用 JSON 原始数据 Laravel 搜索 SQL

[英]Search SQL using JSON Raw data Laravel

I am trying to search sql data from raw that hold json data.我正在尝试从包含 json 数据的原始数据中搜索 sql 数据。 I am trying to search query by using REGEX but its not working and I also want to know is there is a smart way to do that please tell me.我正在尝试使用 REGEX 搜索查询,但它不起作用,我也想知道是否有一种聪明的方法可以做到这一点,请告诉我。

"Riyaz" is value I want - exact value to make sure it search only that keyword. “Riyaz”是我想要的值 - 确保它只搜索该关键字的确切值。

    $customerlist=Customer::where("group", "REGEXP", '^Riyaz$')
       ->orderBy('id', 'DESC')
       ->get();

// DB JSON = {"name":["Riyaz","Saifi"]}

You should use the whereJsonContains query operator to perform a search on a JSON object:您应该使用whereJsonContains查询运算符对 JSON object 执行搜索:

$customersList = Customer::whereJsonContains('group->name', 'Riyaz')
    ->orderBy('id', 'DESC')
    ->get();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM