使用Laravel Fluent Query Builder,有没有办法根据2个子字符串搜索文本?

时间:2015-02-24 02:55:48

标签: php mysql laravel fluent

假设数据库中的字符串如下所示:

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”和“狗”。预期结果将是:

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);

1 个答案:

答案 0 :(得分:1)

为您提供这些结果的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.'%');
}