title: Making a Laravel 5.4 query on a JSON field containing a JSON array
body: <p>i'm trying to query a JSON field containing an array of values.
  For exemple sake we'll name the table "<strong>User</strong>" and the field "<strong>Friends</strong>". Here's how the Friends field looks like :</p>
  
  <pre><code>[{
      "id": 1,
      "img": "img-1.jpg",
      "name": "Name 1"
  },
  {
      "id": 2,
      "img": "img-2.jpg",
      "name": "Name 2"
  },
  {
      "id": 3,
      "img": "img-3",
      "name": "Name 3"
  }]
  </code></pre>
  
  <p>So what I would like to do is <strong>on the User table query everything from the Friends field where there is an id equals to 3</strong>.</p>
  
  <p>So something like : <code>User::where('friends-&gt;id', 3)-&gt;orderBy('id', 'desc')-&gt;get();</code>
  Of course, the exemple above works perfectly if the field did not contain an array, so if it was just :</p>
  
  <pre><code>{
      "id": 1,
      "img": "img-1.jpg",
      "name": "Name 1"
  }
  </code></pre>
  
  <p>Desperate, and even though I know it's not very logical, I have tried with "whereIn" : <code>User::whereIn('friends-&gt;id', [3])-&gt;get()</code>. Or stuff like : <code>User::where('friends-&gt;[0]-&gt;id', 3)-&gt;get()</code>, <code>User::where('friends-&gt;[*]-&gt;id', 3)-&gt;get()</code>, <code>User::where('friends-&gt;*-&gt;id', 3)-&gt;get()</code>.</p>
  
  <p>I have also tried with <em>JSON_CONTAINS</em> or <em>JSON_SEARCH</em> : <code>User::whereRaw('JSON_CONTAINS(friends-&gt;"$.id", "3")')-&gt;get()</code> and many different variants but nothing does it.</p>
  
  <p>Before coming here I have read a few interesting articles on the matter (they are listed bellow), but I seem to be the only one who have ever stored a JSON array in a MySQL database, how is that possible ? ^^</p>
  
  <ul>
  <li><a href="https://mattstauffer.com/blog/new-json-column-where-and-update-syntax-in-laravel-5-3/" rel="noreferrer">https://mattstauffer.com/blog/new-json-column-where-and-update-syntax-in-laravel-5-3/</a></li>
  <li><a href="https://themsaid.com/laravel-mysql-json-colum-fast-lookup-20160709" rel="noreferrer">https://themsaid.com/laravel-mysql-json-colum-fast-lookup-20160709</a></li>
  <li><a href="http://www.qcode.in/use-mysql-json-field-in-laravel/" rel="noreferrer">http://www.qcode.in/use-mysql-json-field-in-laravel/</a></li>
  </ul>
  
  <p>So if anyone could help me solve this problem I would really appreciate it.
  Side notes :  my current MySQL version is 5.7.11, so it does support JSON fields and Laravel doesn't throw any errors, it just returns an empty array.</p>