假设我的数据库中有如下所示的字符串:
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 个子字符串?
假设我正在寻找“fox 0”和“dog”。预期结果是:
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.'%');
}
关于php - 使用 Laravel Fluent Query Builder,有没有办法根据 2 个子字符串搜索文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28687430/