Skip to content

Instantly share code, notes, and snippets.

@esilvajr
Last active May 26, 2017 21:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save esilvajr/1e664453639d533cc0a006bc26d275c3 to your computer and use it in GitHub Desktop.
Save esilvajr/1e664453639d533cc0a006bc26d275c3 to your computer and use it in GitHub Desktop.
search with \\ escape in laravel eloquent in mysql
<?php
$name = "Léo";
$name = json_encode($name);
if (strpos($name, '\\u00') !== false) {
//the first bar is the escape and the second is the real bar
//so we have one bar in search
//and in replace 8 bars, because laravel will scape this bars again and we need 4 to select works in mysql :)
$name = str_replace('\\', '\\\\\\\\\\\\\\', $name);
}
$name = str_replace('"', '', $name);
$name = "%".$name."%";
$candidate = Candidate::query()->whereRaw(" name like '". $name . "'");
var_dump($candidate->toSql(), $candidate->get()->toArray());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment