Home  >  Q&A  >  body text

How to determine if JSON value and parameter are not equal in Laravel?

<p>I want to get those records from the database that are not equal to "My Msg". </p> <p>The JSON in my database column is as follows:</p> <pre class="brush:php;toolbar:false;">`{ "msg": "My Msg", "resource_id": "Resourse" }`</pre> <p>I tried <code>JSON_EXTRACT</code> and <code>whereJsonContains</code></p> <p>But no luck, I'm using Laravel 8 with MySQL. </p> <p>The query is as follows:</p> <pre class="lang-php prettyprint-override"><code>$records = DB::table('atble')->select('res_id', 'msg_string')->where('res_id', $param)->whereJsonContains('ret_string->msg', ['elem1','elem2'...])->latest()->first(); </code></pre> <p>The above query gave me no results. </p> <p>Any help would be greatly appreciated. </p>
P粉250422045P粉250422045394 days ago572

reply all(1)I'll reply

  • P粉256487077

    P粉2564870772023-08-27 09:02:59

    You must try this

    $records = DB::table('atble')->select('res_id',
    'msg_string')->where('res_id',
    $param)->whereJsonContains('ret_string->msg', ['object_key' => 'object_value'])->latest()->first();

    Also follow the correct guidelines for WhereJsonContains

    You can also refer to this linkfor more help

    Here is an example, it will help you.

    $array = [0 => 1, 1 => 2, 2 => 3];
    
    // Eloquent
    PaymentTransaction::whereJsonContains('payload->ProductCode- >id',$array)->get();

    reply
    0
  • Cancelreply