假设数据库中有字符串,如下所示:
The quick brown fox 0 jumps over the lazy dog 0.
The quick brown fox 0 jumps over the lazy dog 1.
The quick brown fox 0 jumps over the lazy dog 2.
The quick brown fox 1 jumps over the lazy dog 0.
The quick brown fox 1 jumps over the lazy dog 1.
The quick brown fox 1 jumps over the lazy dog 2.
有没有一种方法可以使用Laravel Fluent Query Builder搜索2个子字符串?
假设我在找"狐狸0"answers"狗"。预期结果是:
The quick brown fox 0 jumps over the lazy dog 0.
The quick brown fox 0 jumps over the lazy dog 1.
The quick brown fox 0 jumps over the lazy dog 2.
这就是我目前拥有的:
$searchStr = ["fox 0", "dog"];
$query->where('text', '=', $searchStr);
提供这些结果的SQL查询条件是where text like '%fox 0%' and text like '%dog%'
。
要翻译成流畅的查询,请执行以下操作:
$query->where('text', 'like', '%fox 0%')->where('text', 'like', '%dog%');
或者,保持你的阵列:
$searchStr = ["fox 0", "dog"];
foreach($searchStr as $str) {
$query->where('text', 'like', '%'.$str.'%');
}