Eloquent Raw, where a query using a similar condition

I use this Raw query to get some search results combining the header and tag columns. my code is as follows

$term="Test";
$clips=  \Clip::whereRaw("caption like '%?%' OR tags like '%?%' ", array($term,$term))->get();
dd($clips);

but using this, I can’t get the results, because the dump doesn’t show the results, where, using the code below, I can get the results:

$term="Test";
$clips=  \Clip::whereRaw("caption like '%$term%' OR tags like '%$term%' ")->get();
dd($clips);

and the dump shows all 5 results that are expected. What am I doing wrong in the first case.

+4
source share
1 answer

, ? . , . % -sign , .

$term="Test";
$clips=  \Clip::whereRaw("caption like ? OR tags like ? ", array('%'.$term.'%','%'.$term.'%'))->get();
dd($clips);

, , ..

$term="Test";
$clips=\Clip::where("caption","like","%".$term."%")->orWhere("tags","like","%".$term."%")->get();
dd($clips);

... scope .

+12

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


All Articles