Laravel JSON where clause case insensitive
<p>I am using L5.2 and MySQL 5.7. I have the following JSON on my meta column in the DB:</p>

<pre><code>{"cellphone": "(687) 638-4512 x22934", "last_name": "Bashirian", "first_name": "Fidel"}
</code></pre>

<p>I am trying to get the following query to run whether the string is uppercase or lowercase:</p>

<pre><code>User::where('meta-&gt;first_name', 'fidel');
</code></pre>

<p>I have tried something like:</p>

<pre><code>User::whereRaw('LOWER(meta-&gt;"$.first_name") = ?', ['fidel']);
</code></pre>

<p>But that is returning null. Any idea on how to do this!?</p>

<hr>

<p>I also wanted to be able to do queries by fullname for example 'fidel bashirian'. I tried with <code>concat()</code> but I am not sure if that works with JSON column types, so I ended up making it work with the following function:</p>

<pre><code>protected function name($name)
{
    $name = explode(' ', $name);

    foreach ($name as $key =&gt; $value) {

        $name[$key] = '%' . strtolower($value) . '%';

        $this-&gt;builder-&gt;where(function ($query) use ($name, $key) {
            $query-&gt;whereRaw('LOWER(meta-&gt;"$.first_name") like ?', [$name[$key]]);
            $query-&gt;orWhereRaw('LOWER(meta-&gt;"$.last_name") like ?', [$name[$key]]);
        });
    }

    return $this-&gt;builder;
}
</code></pre>

<p>With the previous method querying by <code>fidel</code>, <code>Fidel</code>, <code>FIDEL</code>, <code>fidel bashirian</code>, etc.. will all yield results. Even with partial first or last names.</p>