【问题标题】:How can I use wherejsoncontains when table column has array of objects with same keys and different values laravel php当表列具有具有相同键和不同值的对象数组laravel php时,如何使用wherejsoncontains
【发布时间】:2020-02-17 05:07:24
【问题描述】:

我正在尝试检查具有条件的数据是否存在于json格式的列中,如下所示。

我有 2 列 id,test_data

在 test_data 列中它的 jsonvalue 低于

{"test_info":[{"type":"text","name":'sam',"contact":9839383333,"applicant":1,"email":sam93@gmail.com,"address “:“纽约”}, {"type":"textarea","name":'john',"contact":383839283,"applicant":0,"email":"john33@gmail.com","address":"california"}] }

我正在尝试检查 name='sam' 和 type='text' 的数据是否存在于数据中。在下面尝试过一次,但它不适用于对象数组

TestData::select('id')->WhereJsonContains('test_data->test_info->type', ["text"]);

但上述一个不起作用,因为该列包含对象数组。如何检查数据是否存在。

【问题讨论】:

    标签: php arrays json laravel


    【解决方案1】:

    只需替换

    test_data->test_info->type 
    

    test_info[0]['type']
    

    【讨论】:

    • 有时所需的数据可能存在于第二个对象中,然后我无法保留 test_info[1]['type']。它应该是动态的。
    • 好吧,就做这个$data = TestData::select('id')->WhereJsonContains('test_data->test_info->type', ["text"])->first();
    • 你可以像 `$data['type']` 一样调用它
    猜你喜欢
    • 1970-01-01
    • 2014-03-12
    • 2022-08-12
    • 1970-01-01
    • 2018-05-22
    • 2013-05-12
    • 1970-01-01
    • 1970-01-01
    • 2015-01-04
    相关资源
    最近更新 更多