I
I
Igor Tarasov2020-09-12 20:41:54
Laravel
Igor Tarasov, 2020-09-12 20:41:54

How to force whereRaw in Laravel not to quote variables?

$v = 'test';    
$query->whereRaw('(`column_name` RLIKE "?" ...)', [$v]);


Result: (`column_name` RLIKE "'test'" ...)

But you want (`column_name` RLIKE "test" ...), i.e. without extra quotes. I myself am able to decide where I need quotes. The task of whereRaw is to put the variables with escaped quotes inside the variables.

You can do this:

$v = "test";
$v = DB::connection()->getPdo()->quote($v);    
$query->whereRaw('(`column_name` RLIKE ? ...)', [$v]);


Or like this:

$v = 'test';
$v = DB::connection()->getPdo()->quote($v);  
$query->whereRaw('(`column_name` RLIKE ' . $v . ' ...)', []);


But these are crutch methods in my opinion.

Explain why whereRaw works with quotes added. What is the reason?

Well, do I understand correctly that out of the box what I want is not and I need to write my own trait?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
FanatPHP, 2020-09-12
@FanatPHP

hand-face
who in general showed the child whereRaw who this bad pervert uncle is?

$v = 'test';
$query->where('column_name', 'RLIKE', "{$v}");

you don't need whereRaw
and you don't even need quote()
Well, do I understand correctly that out of the box what I want is not and I need to write my own trait?

maybe you better hire a programmer, director?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question