This problem occurs especially when we want to query on a polymorphic related table.As an example, I wanted to find a column value named App\Example\Files by querying with like. But it failed because of the backslash.
While testing, I found that we need "4 backslashes" for the Laravel side and for the mysql side. For the query to work properly.
I also verified this with the toSql () method. As a result, the output of the mysql query was like this and it worked:
select `file` from `examples` where file like '%App\\\\Example\\\\Files%'
I wrote a little helper function to do the backslash correction
before:
DB::table('examples') ->select('file') ->whereRaw("state like '%App\Example\Files%'") ->toSql();
incorrect output:
select `file` from `examples` where file like '%App\Example\Files%'
after
function handle_backslash($value) :string { return str_replace('\\', '\\\\\\\\', $value); } DB::table('examples') ->select('file') ->whereRaw("file like '%" . handle_backslash('App\Example\Files') . "%'") ->toSql();
correct output:
select `file` from `examples` where file like '%App\\\\Example\\\\Files%'
$searchvariable will always/only contain these two wildcards?