Laravel concat in the request (where is the condition)

I am new to laravel (launched today) and am facing the challenge of creating a simple query:

$query->orWhere("CONCAT(`nvp`, ' ', `vpv`)", 'LIKE', "%$this->searchNeedle%"); 

This line above is one of several conditions in an encapsulated request condition. I think other lines are not needed for this case, but tell me if you need to see them.

I found out that the developer decided to add

`

before and after the first or Where / where param, which cause a problem that I cannot use a simple concat, because the line above will result in:

 `CONCAT(`vpv`, ' ', `nvp`)` LIKE ?)' ↑ ↑ this & this 

Since this is automatically added, I cannot delete it without overwriting the laravel-core function, which I will not use. Is there any SQL hack that handles these two `? Something like 1 = 1, you know?

Perhaps you have another solution to get the expected result by comparing one line with two lines in combination?

+6
source share
2 answers

Laravel does some things behind the scenes, for example, adding marks for you.

Fortunately, it also offers several tools that still do your work ...

For this type, DB::raw() usually works very well. Try something like this ...

 $query->orWhere(DB::raw("CONCAT(`nvp`, ' ', `vpv`)"), 'LIKE', "%".$this->searchNeedle."%"); 
+18
source

Use orWhereRaw to execute a raw request:

 $query->orWhereRaw("CONCAT(`nvp`, ' ', `vpv`) LIKE ?", ['%'.$this->searchNeedle.'%']); 
+8
source

Source: https://habr.com/ru/post/973655/


All Articles