title: How to search for encrypted value using Laravel Eloquent ORM?
body: <p>I've got a Laravel codebase which records are encrypted before they are inserted in the Mysql database. The encryption is done using the <a href="https://github.com/defuse/php-encryption/blob/master/docs/classes/Crypto.md" rel="nofollow noreferrer">Crypto methods of the php-encryption library</a>. If I want to find a record based on one of the encrypted values, looping over all the records works:</p>
  
  <pre><code>$records = TheModel::all();
  foreach ($records as $record){
      if ($record-&gt;thefield == $value) {  // thefield is decrypted in the Eloquent model definition
          print $record-&gt;id;
      }
  }
  </code></pre>
  
  <p>Unfortunately this isn't very scalable. The DB is still quite small, but growing quick so I need to change this code to actually do a query. </p>
  
  <p>So I tried the following code:</p>
  
  <pre><code>$encryptedValue = \App\Crypt::encryptData($value);
  $records = TheModel::where('thefield', $encryptedValue)-&gt;get();
  foreach ($records as $record){
      print $record-&gt;id;
  }
  </code></pre>
  
  <p>But this doesn't return anything. So I then found <a href="https://stackoverflow.com/questions/14505673/sql-query-with-binary-data-php-and-mysql">this SO question</a> it is suggested to add <code>'0x'</code>, wrap it in <code>BIN2HEX()</code> or <code>HEX()</code> or add an <code>x</code> before it (like <code>x'abcd'</code>).</p>
  
  <p>I tried adding <code>'0x'</code> (which doesn't work), but I'm not sure how I could incorporate the other ideas in my code.</p>
  
  <p>Does anybody know how I could try out these ideas with my code?</p>