title: Laravel Eloquent Search in JSON
body: <p>I'm having an issue with searching in a JSON column. Laravel 8.x and MySQL 5.7.x</p>
  <p>Given the following column <code>'industry_focus'</code>, I'm trying to match and return all records containing any of the values</p>
  <p><code>[&quot;1&quot;,&quot;2&quot;,&quot;3&quot;,&quot;4&quot;,&quot;5&quot;,&quot;6&quot;,&quot;7&quot;,&quot;8&quot;]</code></p>
  <p>My query looks like this:</p>
  <pre class="lang-php prettyprint-override"><code>$related = Broker::query()
  -&gt;where('broker_name', '!=', null)
  -&gt;where('is_active', '=', true)
  -&gt;where('state', '=', $state-&gt;code);
  
  foreach ($same_industries as $key =&gt; $value) {
      if ($key === 0) {
          $related-&gt;whereJsonContains('industry_focus', (string) $value-&gt;id);
      } else {
          $related-&gt;orWhereJsonContains('industry_focus', (string) $value-&gt;id);
      }
  }
  
  $related = $related
  -&gt;limit(10)
  -&gt;get();
  </code></pre>
  <p>And it produces a raw query looking like this:</p>
  <pre class="lang-sql prettyprint-override"><code>SELECT *
  FROM `brokers`
  WHERE `broker_name` IS NOT NULL
      AND `is_active` = 1
      AND `state` = 'NY'
      AND json_contains(`industry_focus`, '\&quot;2\&quot;')
      OR json_contains(`industry_focus`, '\&quot;3\&quot;')
      OR json_contains(`industry_focus`, '\&quot;4\&quot;')
      OR json_contains(`industry_focus`, '\&quot;5\&quot;')
      OR json_contains(`industry_focus`, '\&quot;6\&quot;')
      OR json_contains(`industry_focus`, '\&quot;7\&quot;')
      OR json_contains(`industry_focus`, '\&quot;8\&quot;')
      OR json_contains(`industry_focus`, '\&quot;9\&quot;')
      OR json_contains(`industry_focus`, '\&quot;10\&quot;')
      OR json_contains(`industry_focus`, '\&quot;11\&quot;')
      OR json_contains(`industry_focus`, '\&quot;12\&quot;')
      OR json_contains(`industry_focus`, '\&quot;13\&quot;')
      OR json_contains(`industry_focus`, '\&quot;14\&quot;')
      OR json_contains(`industry_focus`, '\&quot;15\&quot;')
      OR json_contains(`industry_focus`, '\&quot;16\&quot;')
  limit 10
  </code></pre>
  <p>Unfortunately, this is wrong and is returning all records ignoring any and all conditions. I think there's a parenthesis missing before the first <code>AND json_contains</code> and a closing one at the end? Is this a bug, or am I missing something. Is there a better way to write this?</p>
  <p><strong>EDIT:</strong> Ok, so I managed to fix this myself somehow looking at examples elsewhere. This is what works for me:</p>
  <pre class="lang-php prettyprint-override"><code>$related = Broker::query()
      -&gt;where('broker_name', '!=', null)
      -&gt;where('is_active', '=', true)
      -&gt;where('state', '=', $state-&gt;code);
  
  $related-&gt;where(function ($query) use ($same_industries) {
      foreach ($same_industries as $key =&gt; $value) {
          if ($key === 0) {
              $query-&gt;whereJsonContains('industry_focus', (string) $value-&gt;id);
          } else {
              $query-&gt;orWhereJsonContains('industry_focus', (string) $value-&gt;id);
          }
      }
  });
  
  $related = $related
      -&gt;limit(10)
      -&gt;get();
  
  </code></pre>