0

I have this kind of json array and i want to check stringValue inside value array is null or not plus i want to check it with its id and fielddata is column name

[
  {
    "name": "50a5613e97e04cb5b8d32afa8a9975d1",
    "value": {
      "stringValue": null
    }
  },
  {
    "name": "bb127e8284c84692aa217539c4312394",
    "value": {
      "dateValue": 1549065600
    }
  }
]

query is:

select * 
from field 
WHERE (fielddata->>'name') = '50a5613e97e04cb5b8d32afa8a9975d1' 
   AND fielddata->'value'->>'stringValue' IS NOT NULL;

and I want use this query in laravel5.7

2 Answers 2

1

Try this

$result = \DB::table('field')->where('name', "50a5613e97e04cb5b8d32afa8a9975d1" )->where('value', $stringValue)->get();

if(isset($result)){
    foreach($result as $res){
        if(isset($res['value']->stringValue)){
            // not null case
        }else{
            // null case
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Within a SQL query, I think you want something like this:

select t.*
from the_table t
where exists (select *
              from jsonb_array_elements(t.fielddata) as j(e)
              where e ->> 'name' = '50a5613e97e04cb5b8d32afa8a9975d1' 
                and e -> 'value' ->> 'stringValue' is not null);

The exists sub-query will check every array element and see if at least one element has the specified name and a non-null stringValue. The query will then return the complete row from the table that fulfills the condition.

Online example: https://rextester.com/AGGJNR88809

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.