Last active
May 26, 2017 21:55
-
-
Save esilvajr/1e664453639d533cc0a006bc26d275c3 to your computer and use it in GitHub Desktop.
search with \\ escape in laravel eloquent in mysql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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